views:

83

answers:

2

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?

A: 

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.

Dave DeLong
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....
A: 

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.

Kendall Helmstetter Gelner
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
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.
Kendall Helmstetter Gelner