Snowman
Snowman

Reputation: 32061

KVO on outside class not working

I have a MainViewController with NSString property currentTag. 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:

[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?

Upvotes: 1

Views: 536

Answers (1)

fbernardo
fbernardo

Reputation: 10104

You're doing it wrong, you should do:

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

Upvotes: 6

Related Questions