简体   繁体   中英

iOS: addObserver and superview query

I have a uiview named subview1. 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

    -(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块,就可能存在问题。还要检查是否已声明变量的属性,如果变量位于不同的类中,则要为其设置观察者。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM