[英]iOS: addObserver and superview query
I have a uiview named subview1. 我有一个名为subview1的uiview。 I add this as subview to a couple of other views depending on certain situations.
根据某些情况,我将此作为子视图添加到其他两个视图中。 Now I have the following code
现在我有以下代码
[subView1 addObserver:self forKeyPath:@"superview" options:NSKeyValueObservingOptionNew context:nil];
My problem is the obserValueForKeypath function is never called 我的问题是从不调用obserValueForKeypath函数
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if (self.subView1 == (UIView*)object) {
if ([keyPath isEqualToString:@"superview"]) {
NSLog(@"superview changed %@",change);
}
}
}
Am i doing something wrong here. 我在这里做错什么了吗?
只需检查是否要进入第一个if块,就可能存在问题。还要检查是否已声明变量的属性,如果变量位于不同的类中,则要为其设置观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.