views:

60

answers:

1

I've read over a ton of documentation and tutorials about KVO but I haven't found any that I've been able to abstract for my application. I have a table view that uses a custom UITableViewCell class to provide an interface for turning options on/off. The cell has a UISwitch that I would like to "bind" to my model's boolean properties. I'd like it that when the cell is rendered it should set the on property of the control appropriately for the managed object and when I flip that switch control, the model object will update to the new value.

I started working on it but the first step of what I thought was appropriate is not working.

[switchControl  addObserver:self
    forKeyPath:@"on"
    options:0
    context:NULL];

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    NSLog(@"value changed");
}
+1  A: 

Try this:

[switchControl  addObserver:self
    forKeyPath:@"on"
    options:NSKeyValueObservingOptionNew
    context:NULL];
jamapag
It is generally a good idea to not only pass in a context, but to check against that context to make sure that the event coming through is generated by your observation.
Marcus S. Zarra