postNotificationName not calling observer method

spacebiker picture spacebiker · Mar 13, 2012 · Viewed 9.9k times · Source

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!

Answer

Rams picture Rams · Mar 13, 2012

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];