将 KVO 与自定义 UITableViewCell 和 CoreData 结合使用
我已经阅读了大量有关 KVO 的文档和教程,但我还没有找到任何能够为我的应用程序抽象的文档和教程。我有一个表视图,它使用自定义 UITableViewCell 类来提供用于打开/关闭选项的界面。该单元格有一个 UISwitch,我想将其“绑定”到我的模型的布尔属性。我希望当渲染单元格时,它应该为托管对象适当地设置控件的 on 属性,并且当我翻转该开关控件时,模型对象将更新为新值。
我开始研究它,但我认为合适的第一步并没有奏效。
[switchControl addObserver:self
forKeyPath:@"on"
options:0
context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"value changed");
}
I've read over a ton of documentation and tutorials about KVO but I haven't found any that I've been able to abstract for my application. I have a table view that uses a custom UITableViewCell class to provide an interface for turning options on/off. The cell has a UISwitch that I would like to "bind" to my model's boolean properties. I'd like it that when the cell is rendered it should set the on property of the control appropriately for the managed object and when I flip that switch control, the model object will update to the new value.
I started working on it but the first step of what I thought was appropriate is not working.
[switchControl addObserver:self
forKeyPath:@"on"
options:0
context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"value changed");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
Try this:
您可以使用 addTarget 代替 addObserver,如下所示:
其中目标函数定义如下:
Instead of addObserver you can use addTarget as below:
where target function is defined as follows: