班级为NSNOTIFICAINCHIFICAY观察者?
-
10-10-2019 - |
题
是否可以有一个静态的NSNOTIFICATION观察者(如下面的代码)?我遇到了一些问题,我认为这可能是由于我的Singleton班级结构所致。
我并不总是周围有一个课程实例来收听通知,而是该课程的静态属性坚持使用我的应用程序的生命周期。
- (id)init {
[super init]
[[NSNotificationCenter defaultCenter] addObserver:[self class]
selector:@selector(action:aNotification:)
name:@"NSSomeNotification"
object:nil];
return self;
}
+ (void)action:(NSNotification *)aNotification {
NSLog( @"Performing action" );
}
解决方案
第一个问题可能是您的选择者 - 应该是 @selector(action:)
.
另外,您确定要在 init
(这错过了任何电话 [super init]
, ,这可能是另一个问题)?这意味着您的通知每次创建类实例时都会(重新)注册。您可以考虑实现真正的单例对象而不是类方法。
不隶属于 StackOverflow