Obj-C Cocoa 通知 NSApplicationDidResignActiveNotification
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
initialize
是一个类方法,而不是实例方法。我不确定这一点,但似乎在类方法中使用选择器时,它还假设选择器将是一个类方法(有充分的理由)。 AppController 有一个名为applicationDidResignActive
的实例方法,但没有这样命名的类方法。不要在
+initialize
中注册通知,而是覆盖-init
并在那里注册。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 calledapplicationDidResignActive
, but not a class method named as such.Instead of registering for notifications in
+initialize
, override-init
and register there.