Search code examples
iosobjective-cnsnotificationcenternsnotification

NSNotificationCenter selector method is not called


Hi i am trying to use NSNotification center in my application.The selector method is not being called from the code.I found similar questions in this site like this, but still i am unable to resolve the error.

i am posting notification in appdelegate did finish launching as: [[NSNotificationCenter defaultCenter] postNotificationName:@"ualert" object:self userInfo:userDict];

adding an observer in one of the view controller as:

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

my selector method is:

- (void)remoteNotificationReceived:(NSNotification *)notification
  {
      NSLog(@"Notification: %@", notification.userInfo);

  }

removing observer as:

    - (void)dealloc
   {
       [[NSNotificationCenter defaultCenter] removeObserver:self];
   }

Solution

  • You are posting ualert in applicationDidFinishLaunching which will necessarily occur before your view controller is loaded (and therefore before you have added the observer for the notification).