在ApplicationDelegate.h
中,我有:
@interface appDelegate : NSObject <NSApplicationDelegate>
- (IBAction)showPage:(id)sender;
在buttonController
中,(连接到Xib中按钮的发送操作)我有另一个功能
- (IBAction) clickAction:(id)sender {
NSLog(@"Clicked");
}
我想用按钮触发该功能,例如:
- (IBAction) clickAction:(id)sender {
[showPage:nil];
NSLog(@"Clicked and the page is shown");
}
跨类访问showPage
函数的正确方法是什么?
答案 0 :(得分:3)
从当前应用程序获取委托,然后在其上调用方法。
[(appDelegate*)(NSApplication.sharedApplication.delegate) showPage:nil];
答案 1 :(得分:1)
不要硬操作动作,只需将动作发送给第一响应者。
这是一个占位符对象,在运行时将解析为第一个响应者(输入字段,视图等)并通过chain-of-responsibility pattern搜索链,直到最终要求您的应用代表响应消息。
在此处了解OS X中的事件处理基础知识: Event Architecture