今天遇到了一個需求角寸,在想利用ViewController的Editing屬性的變化來做一些操作的時候专筷,使用KVO的方式只響應(yīng)了第一次。也就是說不響應(yīng)KVO撕攒。
在網(wǎng)上查找了一下陡鹃。在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,也沒有手動發(fā)送KVO的通知擦俐,那就無法實現(xiàn)KVO的監(jiān)聽了脊阴。
又因為UIViewController沒有公開實現(xiàn)setEditing:
所以就沒有辦法了。
建議自己實現(xiàn)setEditing:animated:
然后自己實現(xiàn)KVO蚯瞧,然后這個屬性就可以監(jiān)聽了嘿期。