KVO - 观察枚举时未调用observeValueForKeyPath
我以前使用过 NSNotifications,但这是我第一次尝试在 Cocoa Touch 中使用 KVO。
我的 UITableView 控制器在各种数据源之间切换,因此我将它们封装在不同的 UITableViewDataSource 子类中。我试图让我的视图控制器观察这些 UITableViewDataSource 子类中的特定一个,并跟踪一个名为 loadState
的枚举,该枚举反映了模型的加载状态。
我这样设置观察者:
[self.siteUpdatesDataSource addObserver:self
forKeyPath:@"loadState"
options:0
context:nil];
从调试器中我可以看到观察者已注册:)
(gdb) po [self siteUpdatesDataSource]
<SiteUpdatesTableViewDataSource: 0x651e5a0>
Current language: auto; currently objective-c
(gdb) po [[self siteUpdatesDataSource] observationInfo]
<NSKeyValueObservationInfo 0x651dd70> (
<NSKeyValueObservance 0x651dd10: Observer: 0xc80f1e0, Key path: loadState, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x651dd90>
但是
,我的 viewController 中的observeValueForKeyPath 方法似乎从未被调用。我设置了一个断点,即使我验证枚举已更改,也不会到达该断点。
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
[self.tableView reloadData];
}
我很感激对我所缺少的东西的任何想法。
I've used NSNotifications before but this is the first time I've tried to use KVO in Cocoa Touch.
My UITableView controller switches between a variety of datasources so I've encapsulated them in different UITableViewDataSource subclasses. I'm trying to have my view controller observe a specific one of these UITableViewDataSource subclasses and track an enum called loadState
that reflects the models the load state.
I set the observer like this:
[self.siteUpdatesDataSource addObserver:self
forKeyPath:@"loadState"
options:0
context:nil];
From the debugger I can see that the observer is registered:
(gdb) po [self siteUpdatesDataSource]
<SiteUpdatesTableViewDataSource: 0x651e5a0>
Current language: auto; currently objective-c
(gdb) po [[self siteUpdatesDataSource] observationInfo]
<NSKeyValueObservationInfo 0x651dd70> (
<NSKeyValueObservance 0x651dd10: Observer: 0xc80f1e0, Key path: loadState, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x651dd90>
)
However, my observeValueForKeyPath method in my viewController never seems to be called. I set a breakpoint and nothing ever reaches it even when I validate that the enum has changed.
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
[self.tableView reloadData];
}
I appreciate any thoughts on what I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看不出有什么问题。您确定通过调用合成访问器或使用 KVC 或手动更改
loadState
属性(使用willChangeValueForKey:
和didChangeValueForKey:
通知更改)?Can't see any problem. Are you sure you change
loadState
property by calling synthesized accessor or by using KVC or manually (notifying about changes withwillChangeValueForKey:
anddidChangeValueForKey:)
?