从其他类访问 IBOutlet
我有一个基于文档的可可应用程序,应用程序菜单中的一个项目连接到 IBAction。单击该项目需要执行一项使用主 nib 文件中的 IBOutlet
的任务,该文件使用另一个类 MyDocument
。创建同一类的 2 个对象,每个笔尖一个对象似乎不起作用。我如何访问插座?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
菜单项的操作通常会发送到第一响应者,以便当前选择的任何内容都可以对其进行操作。
听起来这个动作是对当前文档起作用的,那么它应该由文档来实现。在本例中,让菜单将其操作发送给第一响应者,然后将操作方法放入 MyDocument 类中。
如果您尝试发送的操作是自定义操作:在主菜单笔尖中选择“第一响应者”项,添加您的方法名称,然后将菜单项的选择器连接到该操作。
阅读 Responders 部分doc/uid/10000060i-CH1-SW1" rel="nofollow">Cocoa 事件处理指南 了解更多信息。
Actions for menu items are often sent to the first responder so that whatever is currently selected can act on it.
It sounds like this action is something that works on the current document, then it should be implemented by the document. In this case have the menu send it's action to the first responder and then put the action method in the MyDocument class.
If the action you are trying to send is a custom one: in the Main Menu nib select the First Responder item, add your method name, then connect the menu item's selector to the action.
Read the Responders section of the Cocoa Event-Handling Guide for more info.
总结以上内容,在您的 NIB/XIB 文件中,在界面生成器中建立与第一响应者对象的连接,而不是与文件所有者或其他任何对象的连接。潜在的急救人员仍然会向您提供一系列操作。
然后,Cocoa 获取该选择器并查找它,从当前第一响应者的 NSView(如果有)开始,然后是当前正在使用的 NSDocument,然后是它的窗口控制器等,一直到应用程序委托。它检查的第一个对象实际上实现了该方法,它将使用该对象(在使用同一对象对其进行验证之后)。
因此:
-
-
-
在界面构建器中(甚至以编程方式),如果您已将菜单项的“操作”链接到第一响应者上名为“myMenuAction:”的选择器(这相当于以编程方式完成时不指定目标) ),对于上述两个文档子类,将会发生以下情况。
对于 MyDocumentTypeA,当用户选择该菜单项时,将调用 MyDocumentTypeA 的 -myMenuAction: 。由于 MyDocumentTypeB 没有实现这个操作,Cocoa 将继续查找响应者链,直到它到达您的应用程序委托,而应用程序委托确实实现了它,因此它将在这里被调用。
如果 Cocoa 在响应者链中找不到实现该方法的对象,则菜单项保持禁用状态。
To summarize the above, in your NIB/XIB file, in interface builder make the connection to the First Responder object, not to Files Owner or anything else. You'll still be offered a lit of actions across potential first responders.
Cocoa then takes that selector and looks for it, starting with the NSView (if any) that's currently the first responder, then with the NSDocument that's currently in use, then with it's window controller etc etc all the way up to the Application delegate. The first object it checks that actually implements that method, it will use that object (after validating it with that same object).
So:
-
-
-
In Interface builder (or even programmatically), if you've linked the "action" of the menu item to a selector named "myMenuAction:" on the First Responder (which equates to not specifying a target when done programmatically), for the above two document subclasses the following will happen.
For MyDocumentTypeA, when the user selects that menu item, MyDocumentTypeA's -myMenuAction: will be invoked. Since MyDocumentTypeB does not implement this action, Cocoa will continue to look up the responder chain until it gets to your application delegate, which does implement it, so it will be invoked here instead.
If Cocoa finds no objects in the responder chain that implement the method, the menu item remains disabled.
有一种方法可以做到这一点,我在类似的线程中发布了答案: 从其他类(ObjC)访问IBOutlet
There is a way how to do this, I've posted the answer in a similar thread: Access IBOutlet from other class (ObjC)