对于我的生活,我无法让KVO与UISwitch合作.我有一个自定义UITableViewCell,通过Interface Builder添加了UISwitch.我为UISwitch创建了一个IBOutlet并将其链接到theSwitch变量.
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
[theSwitch addObserver:self forKeyPath:@"on" options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"toggled switch");
}
Run Code Online (Sandbox Code Playgroud)
observeValueForKeyPath:ofObject:change:从不调用context!