gpt4 book ai didi

iphone - KVO - 观察枚举时未调用observeValueForKeyPath

转载 作者:行者123 更新时间:2023-12-03 18:35:43 28 4
gpt4 key购买 nike

我以前使用过 NSNotifications,但这是我第一次尝试在 Cocoa Touch 中使用 KVO。

我的 UITableView Controller 在各种数据源之间切换,因此我将它们封装在不同的 UITableViewDataSource 子类中。我试图让我的 View Controller 观察这些 UITableViewDataSource 子类中的特定一个,并跟踪一个名为 loadState 的枚举,该枚举反射(reflect)了模型的加载状态。

我这样设置观察者:

[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];
}

我很感激对我所缺少的东西的任何想法。

最佳答案

看不出有什么问题。您确定通过调用合成访问器或使用 KVC 或手动更改 loadState 属性(使用 willChangeValueForKey:didChangeValueForKey: 通知更改)?

关于iphone - KVO - 观察枚举时未调用observeValueForKeyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277836/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com