Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use as the keypath in KVO?

I have a view controller with a view that changes (for example), and I would like to observe the frame of any view that self.view is set to. Is there any difference between:

[self.view addObserver:self forKeyPath:@"frame" options:0 context:nil];

and

[self addObserver:self forKeyPath:@"view.frame" options:0 context:nil];

For the second one, if the view changes will messages still be recieved when the new view's frame changes, or will it only send messages if the frame of the view that was set when the observer was added?

Is there any way to observe changes to the frame property even if the view of the view controller changes after adding the observer?

like image 250
Jonathan. Avatar asked Feb 13 '12 15:02

Jonathan.


1 Answers

Use the second path. @"view.frame" will notify you about the frame changes even when the "view" itself is changed. Cocoa will add observers for every object in the keyPath "chain" for you automatically (which means every item in the keyPath must be KVO-compatible).

like image 58
Gobra Avatar answered Sep 23 '22 12:09

Gobra