我在我的应用程序 delegate.m 中定义了一个 BOOL,然后在 View controller.h 中定义了
extern BOOL usingInternet;
现在我想在这个值发生变化时调用一个方法,我正在尝试
[usingInternet addObserver:self forKeyPath:@"myData" options:0 context:@"data"];
但是我得到一个编译错误,Bad receiver type BOOL
我如何在每次这个 bool 改变值时调用一个方法?
谢谢
最佳答案
[self addObserver:self forKeyPath:@"usingInternet" options:0 context:nil];
并从中获取结果
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"%@, %@, %@", keyPath, @(usingInternet), change);
}
关于ios - 变量改变值时调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281692/