跨类访问NSApplicationDelegate方法

时间:2014-07-21 01:52:58

标签: objective-c macos cocoa

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函数的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

从当前应用程序获取委托,然后在其上调用方法。

[(appDelegate*)(NSApplication.sharedApplication.delegate) showPage:nil];

答案 1 :(得分:1)

不要硬操作动作,只需将动作发送给第一响应者。

这是一个占位符对象,在运行时将解析为第一个响应者(输入字段,视图等)并通过chain-of-responsibility pattern搜索链,直到最终要求您的应用代表响应消息。

在此处了解OS X中的事件处理基础知识: Event Architecture