从其他类访问 IBOutlet

发布于 2024-10-01 18:27:58 字数 168 浏览 5 评论 0 原文

我有一个基于文档的可可应用程序,应用程序菜单中的一个项目连接到 IBAction。单击该项目需要执行一项使用主 nib 文件中的 IBOutlet 的任务,该文件使用另一个类 MyDocument。创建同一类的 2 个对象,每个笔尖一个对象似乎不起作用。我如何访问插座?

I have a document based cocoa application with an item in the application menu hooked up to an IBAction. Clicking the item needs to perform a task that uses an IBOutlet in the main nib file which is using another class, MyDocument. Creating 2 objects of the same class, one in each nib seems to not be working. How can I access the outlet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2024-10-08 18:27:58

菜单项的操作通常会发送到第一响应者,以便当前选择的任何内容都可以对其进行操作。

听起来这个动作是对当前文档起作用的,那么它应该由文档来实现。在本例中,让菜单将其操作发送给第一响应者,然后将操作方法​​放入 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.

贩梦商人 2024-10-08 18:27:58

总结以上内容,在您的 NIB/XIB 文件中,在界面生成器中建立与第一响应者对象的连接,而不是与文件所有者或其他任何对象的连接。潜在的急救人员仍然会向您提供一系列操作。

然后,Cocoa 获取该选择器并查找它,从当前第一响应者的 NSView(如果有)开始,然后是当前正在使用的 NSDocument,然后是它的窗口控制器等,一直到应用程序委托。它检查的第一个对象实际上实现了该方法,它将使用该对象(在使用同一对象对其进行验证之后)。

因此:

@interface MyDocumentTypeA : NSDocument {
}

-(void)myMenuAction:(id)sender;

-

@interface MyDocumntTypeB : NSDocument {
}

// -myMenuAction: not implemented here

-

@interface MyApplicationDelegate ... {
}

-(void)myMenuAction:(id)sender;

-

在界面构建器中(甚至以编程方式),如果您已将菜单项的“操作”链接到第一响应者上名为“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:

@interface MyDocumentTypeA : NSDocument {
}

-(void)myMenuAction:(id)sender;

-

@interface MyDocumntTypeB : NSDocument {
}

// -myMenuAction: not implemented here

-

@interface MyApplicationDelegate ... {
}

-(void)myMenuAction:(id)sender;

-

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.

情绪 2024-10-08 18:27:58

有一种方法可以做到这一点,我在类似的线程中发布了答案: 从其他类(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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文