我正在尝试从 View Controller 观察单例中的字符串。
在我的 View Controller 中,我在 viewDidLoad 中执行此操作:
Singleton *singleton = [Singleton sharedSingleton];
[singleton addObserver:self
forKeyPath:@"testString"
options:NSKeyValueObservingOptionNew
context:nil];
..此代码位于 View Controller 的底部:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"recieved");
}
单例中的testString在5秒后更改,但没有调用observeValueForKeyPath..我做错了什么?
最佳答案
使用属性,例如self.testString = newString
而不是 testString = newString
,否则 KVO 将不起作用。
关于objective-c - 从 View Controller 观察单例的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8095823/