swift - 同时将菜单项连接到第一响应者和多个 View Controller

标签 swift xcode first-responder nsviewcontroller

我有一个主视图 Controller ,里面有一个容器 View 。我能够在主视图 Controller 中创建一个 IBAction,并成功地将菜单项连接到第一响应者中的该操作。但是当将不同的菜单项连接到容器 View Controller 操作时,菜单项保持灰色。

enter image description here

在主视图 Controller 中:

@IBAction func outerMenuTest(_ sender: Any) {
    print("outerMenuTest")
}

在容器 View Controller 中:

@IBAction func containerMenuTest(_ sender: Any) {
    print("containerMenuTest")
}

如何将菜单项同时连接到容器 View Controller 中的 View Controller ?我需要启用两个菜单项。

最佳答案

将菜单项连接到除应用程序委托(delegate)之外的任何内容的最简单方法是在 Storyboard 中使用 First Responder。在建立连接之前,您需要已经创建了 IBAction 是合适的类。您可能还需要清理和/或构建项目以强制 Xcode 索引所有 IBAction

如果 Xcode 正在运行并且操作在 Xcode 视为潜在第一响应者的类中,当您控制从菜单项拖动到大纲中的 First Responder 时,它将显示在选项列表中在 Canvas 的左侧:

enter image description here

First Responder 是整个响应链的代理。当有东西指向它时,系统将从第一个响应者开始,沿着响应者链向上移动,直到找到一个响应指定选择器的响应者。只要您的容器 View 在响应链中,它就会收到消息。

关于swift - 同时将菜单项连接到第一响应者和多个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821197/

相关文章:

ios - 视觉格式语言中的 "|"和 [superview] 有什么不同?

swift - 自定义类 SpriteKit

c++ - 无法在 Mac OSX 上设置 OpenCL

iPhone/xCode - 第四次运行动画代码时的 EXC_BAD_ACCESS

iphone - iOS 应用程序 "next"键不会转到下一个文本字段

cocoa - 为什么设置initialFirstResponder没有效果?

Swift 3 双舍入函数

swift - Console 在 Swift 中继续输出包装为 Optional 的字符串,为什么?

ios - UITextField 的键盘不会在 Swift 中第一次点击时出现

iOS swift : CIDetectorTypeRectangle is not convertible