I have an appDelegate, that has a property myWindow of MyWindowClass. I need observe bool property from myWindow. Than I have a CustomViewController, that needs to observe for bool value changes. If I want to addObserver I do following in ViewController:
LayerWindow *w = ((AppDelegate*)[UIApplication sharedApplication].delegate).window;
[w addObserver:self forKeyPath:@"touchInsideLayerWindow" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
In ViewController I have
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
method defined, also in header file.
In WindowClass I have following code:
[self setValue:[NSNumber numberWithBool: YES]forKey:@"touchInsideLayerWindow"];
NSLog(@"isTouchInside %@", self.touchInsideLayerWindow ? @"YES" : @"NO");
Method observeValueForKeyPath in ViewController is never called. Does anyone know, what is wrong? Thanks
Looks like one problem is that you said the property is a BOOL, but you are trying to set it to an NSNumber in the setValue call.
Second: if you make your setter with @synthesize, then KVO is automatically supported as long as you use dot-syntax.
self.touchInsideLayerWindow = YES;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With