简体   繁体   English

外部课堂上的KVO无法正常工作

[英]KVO on outside class not working

I have a MainViewController with NSString property currentTag. 我有一个带有NSString属性currentTag的MainViewController。 So I do: 所以我做:

[self addObserver:self forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil];

and this works fine, and the changes do get handled properly. 这样就可以正常工作,并且所做的更改也得到了正确处理。 However, I have another class SecondViewController, and I also want it to observe MainViewController's currentTag, so in the SecondViewController's viewDidLoad method, I do: 但是,我还有另一个类SecondViewController,并且我还希望它观察MainViewController的currentTag,因此在SecondViewController的viewDidLoad方法中,我这样做:

[self addObserver:self.mainViewController forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil];

However, this one doesn't get handled for some reason. 但是,由于某种原因,这一操作没有得到处理。 It never gets called. 它永远不会被调用。 I want to make sure my syntax and form is correct before looking elsewhere in my code for the problem. 我想确保我的语法和格式正确,然后再在代码中查找该问题。 Does this code seem ok? 这段代码看起来还好吗?

您做错了,应该这样做:

[self.mainViewController addObserver:self forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM