I’m trying to use NSNotification in my Storyboard.
This code work when I don’t use storyboard.
this is in ViewController1:
- (IBAction) buttonClickedListener2:(id)sender {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];
}
And this is in ViewController2:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
NSLog(@"initWithNib");
}
return self;
}
- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
Is it anything wrong with the codes? As I said I want to make it work in my Storyboard.
I appreciate if you give me some toturials about how to use NSNotification in STORYBOARD.
Storyboards don’t use
initWithNibName, so the part of your code where you add your observer doesn’t get run. You should be able to see this by setting a breakpoint. A fix would be to replacewith