18

I have a ViewController creating an instance of a UIView, and then I register an observer with the instance, such that

logoAnimation = [[MainLogoAnimation alloc] init];
[logoAnimation addObserver:self forKeyPath:@"patrocinioDidLoad" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];

then, in the same file, I have:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
 NSLog(@"%@ \n %@ \n %@ \n ",keyPath,object,change);
}

But, although I have checked and double-checked that logoAnimation.patrocinioDidLoad has changed, observeValueForKeyPath never gets called...

Am I missing something?

Thanks for the help!

Antonio

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Antonio
  • 863
  • 1
  • 11
  • 23

1 Answers1

20

Solved it: I was setting patrocinioDidLoad in logoAnimation directly, without using standard getters and setters. In logoAnimation,

patrocinioDidLoad = YES;

didn't work, whereas

self.patrocinioDidLoad = YES;

did!

Antonio
  • 863
  • 1
  • 11
  • 23
  • thanks you solved my problem For me, something like logoAnimation = [[MainLogoAnimation alloc] init]; didn't work but self.logoAnimation = [[MainLogoAnimation alloc] init]autorelease]; works. – ssj Apr 21 '11 at 03:58
  • So you always have to put "self" in front if you want to use the setters and getters made by @synthesize? – gonzobrains Jul 12 '11 at 02:28
  • WOW! After hours of testing my code to find why obserValueForKeyPath wasn't ever being called, I finally found that this was why. Why doesn't Apple bold it in the docs that one must go through the setter (and thus use the property instead of the ivar) for it to work? – ArtOfWarfare Apr 27 '12 at 03:09