如何使嵌入式视图控制器成为响应者链的一部分? [英] How to make embedded view controller part of the responder chain?
问题描述
我正在使用情节提要开发Mac应用程序.我有一个显示NSViewController
作为其内容的窗口,其中包含一个嵌入NSSplitViewController
的容器视图控制器".
I am developing a Mac app using storyboards. I have a window that presents an NSViewController
as its contents, which contains a "container view controller" that embeds an NSSplitViewController
.
NSSplitViewController
的预期行为是使其成为响应者链的一部分,以便在第一个响应者上触发toggleSidebar
动作的菜单项实际上会使标记为侧边栏的NSSplitViewController
项折叠
The expected behaviour is for the NSSplitViewController
to be part of the responder chain, such that a menu item that triggers the toggleSidebar
action on the first responder actually collapses the item of the NSSplitViewController
that's marked as a sidebar.
但是,这根本不会发生,并且菜单项保持禁用状态.所以我的问题是,如何让NSSplitViewController
成为响应者链的一部分?
However, this simply does not happen and the menu item remains disabled. So my question is, how can-I get the NSSplitViewController
to be part of the responder chain?
推荐答案
I noticed that maybe some of these solutions have worked, but i adapted a more general purpose answer from https://stackoverflow.com/a/30938725/6938357.
我在NSViewController
上进行了扩展,以寻找补充目标.可在NSSplitViewController
以及带有子级的任何常规NSViewController
上使用.
I made an extension on NSViewController
to look for supplemental targets. Works on NSSplitViewController
as well as any general NSViewController
with child(ren).
extension NSViewController {
open override func supplementalTarget(forAction action: Selector, sender: Any?) -> Any? {
if let target = super.supplementalTarget(forAction: action, sender: sender) {
return target
}
for child in children {
var target = NSApp.target(forAction: action, to: child, from: sender) as? NSResponder
if target?.responds(to: action) == false {
target = child.supplementalTarget(forAction: action, sender: sender) as? NSResponder
}
if target?.responds(to: action) == true {
return target
}
}
return nil
}
}
如果只希望它在单个视图控制器上进行搜索,请将该实现放在那里.此扩展适用于 all NSViewController
s及其子类.
If you only want this to search on a single view controller, put this implementation there instead. This extension applies to all NSViewController
s and its subclasses.
这篇关于如何使嵌入式视图控制器成为响应者链的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!