今天遇到了一个需求,在想利用ViewController的Editing属性的变化来做一些操作的时候,使用KVO的方式只响应了第一次。也就是说不响应KVO。
在网上查找了一下。在stackoverflow上找到了一个提问。
[Observing the editing property of a UITableViewController]
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?
下面第一个回答解释了原因,
KVO-compliance is achieved by, which KVO will wrap automatically, or manually posting KVO notifications by sending itself will
/didChangeValueForKey:
messages.
最要就解释了,如果这个属性没有遵循KVC 的setter,也没有手动发送KVO的通知,那就无法实现KVO的监听了。
又因为UIViewController没有公开实现setEditing:
所以就没有办法了。
建议自己实现setEditing:animated:
然后自己实现KVO,然后这个属性就可以监听了。
网友评论