I’m trying to understand key value observation in iOS but I think I’m not doing something correctly.
As an idea, I tried to add an observer to a view controller’s property (a view connected with an IBOutlet). This view (tableIndicator) is animated so I wanted to see if I can get the observer to react when the view’s frame changes.
So I did the following, inside the view controller’s viewDidLoad:
[tableInidicator addObserver:self forKeyPath:@"frame" options:0 context:nil];
tableIndicator is my view/class property, I’m adding the view controller (self) as the observer, 0 for the default options and frame as the key value being observed.
Then, I’m waiting to see if this function is triggered as the frame changes:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"value changed");
}
…but nothing happens.
I’m not necessarily looking for fix to this code since it serves no purpose other than for me to understand it and I would be really grateful if someone could point out to me what I’m doing wrong. Some good examples/tutorials would be awesome too.
The ones I found ( http://iphonedevelopment.blogspot.ro/2009/02/kvo-and-iphone-sdk.html / http://nachbaur.com/blog/back-to-basics-using-kvo ) did not cover such cases. They were only observers applied to a class to watch for one of its properties, not for the property of a (custom)object inside a class, something that I think would be more useful for me.
Thank you in advance
[edit]For those who will miss my comment on the accepted answer:
Changing a view’s center will apparently not trigger an observer for the frame property. You have to change the frame itself.
Check that your IBOutlet is connected correctly, probably the tableIndicator ivar points to a nil.
Consider this simple code below, it works. It just creates a window, add a red square on it, then register using KVO the object to be notified for frame change. Finally it instantiates a button: each time you tap on it the frame is reduced by size, and the notification is triggered correctly (you will see the message in the debug console).
So you must check your code.