0

I just dont understand, I have used notifications a trillon times, but today I cant seem to get notifications to send from a particular object. This is in a view controller...

- (void)buttonPressed:(UIButton*)sender {
NSLog(@"buttonPressed");
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self];
}

and this is in another view controller(in the init)..

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"test" object:nil];

and the method..

-(void)test:(NSNotification *)notification{
NSLog(@"it works! ");
}

...just standard stuff, now if i post the notification from any other object it works fine, just wont post from this one view controller! It even logs "buttonPressed" so it's not like the button is broken. I just dont get it.

Any ideas?

1
  • I give in! I wasted two days on this. Very very strange indeed. I rewrote it and used delegation instead. All my other notifications work perfectly fine except this one. Weird.!
    – user139816
    Nov 18, 2009 at 10:30

2 Answers 2

0

Is this a view controller that you created by dragging and dropping in Interface Builder? If so, then the -init method won't be called (the -initWithCoder will be). However, if you need to set up stuff from the interface, override the -awakeFromNib method and put your setup code in there.

1
  • Sorry that was me stupidly abbreviating my explanation. I have a log after 'addObserver' and that piece of code runs fine. Thats the thing all the logs fire, and in the correct order, yet no communication via notification. I'm baffled. I'm going to rewrite the whole thing, to see if anything jumps out at me....
    – user139816
    Nov 17, 2009 at 16:05
0

Have you tried passing nil instead of self for the object: argument when you send? I thought nil meant you would get every notification with that name but it may not be the case.

Also, try subscribing to the notification just before you send it and see if that gets called.

2
  • He does use nil when adding the observer, so the notifications are not filtered to only include those with a particular object. Posting a notification with an object shouldn't change how it's being received.
    – Brad Larson
    Nov 17, 2009 at 17:59
  • That's what I thought too, but it never hurts to try, and might jog him to think of something else he might be doing wrong. Nov 18, 2009 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.