在Objective-C中观察ANY类属性的变化 [英] Observing a Change to ANY Class Property in Objective-C

查看:88
本文介绍了在Objective-C中观察ANY类属性的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,当Objective-C类中的任何属性更改时,是否有办法接收一般通知?我知道我可以使用KVO监视特定的属性更改,但是每当任何setProperty:消息发送到我的班级时,我都需要调用特定的方法.我希望能够收到通用通知,而不必担心具体修改了哪个属性.

如果这有助于弄清为什么我要这样做,我将使用一些快速表滚动代码,可在此处找到:解决方案

正如Chuck所说,您可以创建一个依赖键,或者当然也可以直接观察所有属性(这比重载setter的工作量要小). /p>

使用Objective-C运行时,如果您仅使用属性,则可以使用http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/

Part of the process of accomplishing this is that whenever a property in a table view cell is modified, [ self setNeedsDisplay ] needs to be called. I'd rather not have to override the setter methods for every property in my class just to make this call.

解决方案

As Chuck notes, you can create a dependent key, or of course you can directly observe all the properties (which is less work than overloading the setters).

Using the Objective-C runtime, if you exclusively use properties, you can automate this process using class_copyPropertyList(). But I'd probably only do this if this problem comes up a bit for you. If you only have one instance of this problem, it's probably easier and safer and more maintainable just to directly observe the list of properties unless you feel like working in the ObjC runtime.

这篇关于在Objective-C中观察ANY类属性的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆