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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With