Obj-C Cocoa 通知 NSApplicationDidResignActiveNotification

发布于 2024-10-19 21:11:41 字数 784 浏览 0 评论 0原文

我有一个名为 AppController.h/m 的类,我想在发送 NSNotificationDidResignActiveNotification 时做一些事情。 所以我在 AppController.m 中编写了这段代码:

-(void) initialize(){
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidResignActive:)
                                                     name:NSApplicationDidResignActiveNotification
                                                   object:nil ];
}

然后

-(void) applicationDidResignActive (NSNotification*) note{
    NSBeep();
}

问题是该方法没有执行,我在控制台中得到了这个:

+[AppController applicationDidResignActive:]: unrecognized selector sent to class 0x61c4

我不知道问题出在哪里:你能帮我吗?
谢谢!

I've a class called AppController.h/m I want to make something when the NSNotificationDidResignActiveNotification is sent.
So i wrote this code in AppController.m:

-(void) initialize(){
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidResignActive:)
                                                     name:NSApplicationDidResignActiveNotification
                                                   object:nil ];
}

and then

-(void) applicationDidResignActive (NSNotification*) note{
    NSBeep();
}

The problem is that the method isn't executed and i get this in the Console:

+[AppController applicationDidResignActive:]: unrecognized selector sent to class 0x61c4

I can't get where the problem is: could you help me?

Thank you!

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

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

发布评论

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

评论(1

幸福%小乖 2024-10-26 21:11:41

initialize 是一个类方法,而不是实例方法。我不确定这一点,但似乎在类方法中使用选择器时,它还假设选择器将是一个类方法(有充分的理由)。 AppController 有一个名为 applicationDidResignActive 的实例方法,但没有这样命名的类方法。

不要在 +initialize 中注册通知,而是覆盖 -init 并在那里注册。

- (void)init
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidResignActive:)
                                                     name:NSApplicationDidResignActiveNotification
                                                   object:nil ];
}

initialize is a class method, not an instance method. I don't know this for sure, but it seems that when using a selector in a class method, it also assumes that selector will be a class method (for good reason). AppController has an instance method called applicationDidResignActive, but not a class method named as such.

Instead of registering for notifications in +initialize, override -init and register there.

- (void)init
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidResignActive:)
                                                     name:NSApplicationDidResignActiveNotification
                                                   object:nil ];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文