Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postNotificationName not calling observer method

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!

like image 542
spacebiker Avatar asked Mar 13 '12 10:03

spacebiker


2 Answers

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];
like image 163
Rams Avatar answered Oct 28 '22 07:10

Rams


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?

like image 5
Stephen Darlington Avatar answered Oct 28 '22 06:10

Stephen Darlington