views:

51

answers:

1

Why can't I observe the editing property of an instance of UITableViewController?

I'm using the following code:

[self addObserver:self 
       forKeyPath:@"editing" 
          options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
          context:NULL];

And have implemented the method:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context

... but the observeValueForKeyPath method is never called when this value changes.


According to Apple's Ensuring KVC Compliance section:

For properties that are an attribute or a to-one relationship, this requires that your class:

  • Implement a method named -<key>, -is<Key>, or have an instance variable <key> or _<key>.
  • If the property is mutable, then it should also implement -set<Key>:.
  • Your implementation of the -set<Key>: method should not perform validation.
  • Your class should implement -validate<Key>:error: if validation is appropriate for the key.

The documentation for the editing property, states that it is defined as:

@property(nonatomic, getter=isEditing) BOOL editing

Since this property is not mutable, the only bullet point it must conform to is the first one (i.e. that there is an -is<Key> method defined, for example). You can see that it does conform to this by looking at the declaration of the property, and noticing that there is an isEditing method defined. Thus, it should be Key Value Observing compliant. How come it isn't working?

+3  A: 

You're confusing Key-Value Coding compliance with Key-Value Observing compliance. The property is KVC-compliant, which means you can use [myViewController valueForKey:@"editing"] to access it (if you like typing), but this does not mean it is KVO-compliant.

KVO-compliance is achieved by the object either implementing a KVC-compliant setter (bullet points 2 and 3), which KVO will wrap automatically, or manually posting KVO notifications by sending itself will/didChangeValueForKey: messages.

UIViewController and UITableViewController do not publicly implement setEditing:; if they don't implement it at all, then KVO wrapping it automatically is out. That leaves manual notifications. If you're not getting any KVO notifications for the property (and you are actually hitting that addObserver:forKeyPath:options:context: message), that suggests that those classes neither privately implement setEditing: nor manually post KVO notifications.

Therefore, the property is not observable.

If the only way anything ever sets the editing property is by sending the controller a setEditing:animated: message, then you can override setEditing:animated: and send the KVO notifications yourself from your implementation, and then the property will be observable.

Peter Hosey
Thanks for the informative answer. I have seen that page that you link to, but unfortunately, it doesn't explain it as well as you have.
Senseful