postNotificationName not calling observer method

10,011

Solution 1

just change a way..

First add observer

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

Then Post Notification

 [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:items];

Finally remove in viewWillDisappear

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ProcessDidComplete" object:nil];

Solution 2

Your code looks okay, which makes me wonder where in your app delegate you post the notification?

If you post the notification before you add the observer in the view controlller, then the notification will never be received. Can you not send the message to the main view controller directly, i.e., as a property, rather than using notifications?

Share:
10,011
spacebiker
Author by

spacebiker

simplicity is the ultimate sophistication

Updated on June 14, 2022

Comments

  • spacebiker
    spacebiker about 2 years

    I am trying to call a method within an uiview from AppDelegate using the NSNotificationCenter to no avail..

    AppDelegate.m

        [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDidComplete" object:items];
    

    Then via MainStoryboard, the main view is loaded which controller class is MainViewController

    in MainViewController.h viewDidLoad i have

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

    and then the method

    - (void) ProcessDidComplete:(NSNotification *)pNotification
    

    but it never gets called.

    Thanks for any help!

  • spacebiker
    spacebiker over 12 years
    Woah! that was fast. Thanks for your reply. You pointed me in the right direction. postNotificationName is being called before the observer is created, so now the question is.. taking into account i load the views using the storyboard, how could i make it to addObserver before posting the notificacion as the postNotificationName is called in AppDelegate's didFinishLaunchingWithOptions and the observer is added in the viewDidLoad from the MainView.. thanks in advance
  • spacebiker
    spacebiker over 12 years
    hi mate, thanks for the reply. the notification is post in the didFinishLaunchingWithOptions and the observer is created in the MainView's viewDidLoad, because i am using story board, the MainView is loaded too late. Do you have any suggestion ?
  • Rams
    Rams over 12 years
    I haven't implemented storyboard..Better you try addobserver in appdidfinishlaunching method.