为NSNotification注册公告 [英] Registering a bool for a NSNotification
问题描述
我试图围绕NSNotification努力,但似乎无法使其正常工作.认为我误会了如何注册通知.
I'm trying to wrap my head around NSNotification but can't seem to get it to work. Think I'm misunderstanding how to register for an notification.
我在连接管理器类中有一个bool作为属性.初始化时,我通过几台服务器进行身份验证,并检查是否可以访问外部URL(应用程序将主要用于公司的Intranet,并且并不总是可以进行外部连接)
I have a bool as a property in my connection manager class. At initialisation I authenticate with a few servers and check if I can access an external URL (App will mainly be used on company intranet and an external connection isn't always possible)
如果BOOL属性无法访问连接,它将从YES更改为NO,并且由于可以随时响应,因此我认为最好在更改时注册通知.该属性称为externalConnectionAvailable
The BOOL property will be changed from YES to NO if it cannot access the connection and as this can be responded at any time I thought it would be best to register a notification for when it changes. The property is called externalConnectionAvailable
[ConnectionManager addObserver:self forKeyPath:@"externalConnectionAvailable" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL];
并具有方法:
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"observer called");
}
但是不会被调用.我在做些完全错误的事情吗?
But this doesn't get called. Am I doing something completely wrong?
谢谢
推荐答案
这很愚蠢.我只是通过调用externalConnectionAvailable而不是self.externalConnectionAvailable
It was something very stupid. I was just changing the property by calling externalConnectionAvailable not self.externalConnectionAvailable
这篇关于为NSNotification注册公告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!