objective-c - 从 View Controller 观察单例的数据

标签 objective-c ios singleton key-value-observing

我正在尝试从 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/

相关文章:

objective-c - 在 Cocoa 应用程序上接受拖动的文件

ios - UIDatePicker EventValueChanged 未显示实际日期/时间

iphone - Flurry Analytics,基本分析问题

c# - 单例 - 我可以创建多个实例

python - 什么时候在 python 中使用单例?

ios - 更改任何 swift 代码后 Umbrella 头文件未更新

iphone - 创建 http 服务器并通过热点连接

iphone - AFNetworking:setImageWithURLRequest 未设置图像

objective-c - 在没有 bonjour 的情况下列出 wifi 网络 iOS sdk 上所有设备的 IP 地址?

c++ - 带有引用变量的单例