KVO 控制器(UIViewController)的 Editing属性

今天遇到了一个需求,在想利用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,然后这个属性就可以监听了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 两年三年换车。 过户次数少,最多一次。 公里数少, 小事故少, 价格便宜。 宝马三系。 听老婆的!(或者奔驰) 怎...
    厦大平兄探险记阅读 227评论 0 0
  • 目录 上一章 “这腌臜杀不尽的鞑子!”王方旋跺脚狠狠道:“难道当时我汉人真就懦弱,没有英雄豪杰起来杀尽鞑子,复江山...
    步六孤大将军阅读 336评论 16 25
  • 我不知道哪种要惨一點 愚蠢而不自知 還是自知愚蠢而無可奈何 或許某種情況下我們都是愚人 只不過患病的程度不同罷了 ...
    我是独一无二的我阅读 210评论 0 0
  • 要根签,然后需要提供资产证明。我发现自己没房没车没五万存款……没有钱连签证都下不来。暑假还是乖乖赚钱吧,有钱就有安...
    直直男孩的女孩阅读 204评论 0 0