[英]KVO not working with UISwitch
對於我的生活,我無法讓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");
}
observeValueForKeyPath:ofObject:change:從不調用context!
我不確定,但UISwitch可能不符合KVO標准。
無論如何,因為你可以只使用控制事件:
[theSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
// ...
- (void)switchChanged:(UISwitch *)sender {
if (sender.on) {
// ...
}
}
添加觀察者時,可能尚未初始化切換。 嘗試在awakeFromNib中添加觀察者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.