Obj-C Cocoa通知NSApplicationDidResignActiveNotification [英] Obj-C Cocoa Notification NSApplicationDidResignActiveNotification

查看:492
本文介绍了Obj-C Cocoa通知NSApplicationDidResignActiveNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ];
}

,然后

-(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!

推荐答案

初始化一个类方法,而不是一个实例方法。我不知道这是肯定的,但似乎当在类方法中使用选择器,它也假设选择器将是一个类方法(很好的原因)。 AppController有一个名为 applicationDidResignActive 的实例方法,但不是一个类方法。

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.

+ initialize ,重写 -init 并在那里注册。

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 ];
}

这篇关于Obj-C Cocoa通知NSApplicationDidResignActiveNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆