Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO on bool property, not invoke observeValueForKeyPath

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

like image 698
Skodik.o Avatar asked Jan 29 '26 01:01

Skodik.o


1 Answers

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;
like image 51
Walt Sellers Avatar answered Jan 31 '26 15:01

Walt Sellers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!