xcode 如何将 IBAction 和菜单项连接到 First Responder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37168040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect IBAction and menu item to First Responder?
提问by C. Pave
In Xcode storyboard I am trying to connect IBAction from ViewController to menu item in application scene (the menu bar).
在 Xcode 故事板中,我试图将 IBAction 从 ViewController 连接到应用程序场景中的菜单项(菜单栏)。
I found a solution telling that I should connect the menu item to First Responder. Then the First Responder to IBAction, since First Responder presents on every scenes. So, the First Responder can receive action from menu bar first as nil then chain it to IBAction.
我找到了一个解决方案,告诉我应该将菜单项连接到 First Responder。然后是IBAction的First Responder,因为First Responder出现在每个场景中。因此,First Responder 可以首先从菜单栏接收操作为 nil,然后将其链接到 IBAction。
I however cannot figure out how to connect menu item to First Responder and First Responder to IBAction. When I ctrl+dragged menu item to First Responder, it shows only preset actions. And I cannot ctrl+drag ViewController to First Responder on viewController scene at all.
但是,我无法弄清楚如何将菜单项连接到 First Responder 以及如何将 First Responder 连接到 IBAction。当我 ctrl+drag 菜单项到 First Responder 时,它只显示预设操作。而且我根本无法在 viewController 场景中按 ctrl+drag ViewController 到 First Responder。
How do I connect IBAction and menu item to First Responder?
如何将 IBAction 和菜单项连接到 First Responder?
回答by Khundragpan
When you create an IBAction either by dragging from IB or writing yourself, those actions are added to the actions list in the FirstResponder object in the IB. Next step is to drag a menu item from object library to menu bar and then right click firstResponder object and search for the action you want and connect it to menu bar item, you recently added.
当您通过从 IB 拖动或自己编写来创建 IBAction 时,这些操作将添加到 IB 的 FirstResponder 对象中的操作列表中。下一步是将菜单项从对象库拖到菜单栏,然后右键单击 firstResponder 对象并搜索您想要的操作并将其连接到您最近添加的菜单栏项。