smartsanja
smartsanja

Reputation: 4550

How to use NSNotification

In my application there is two viewControllers as FirstViewController and DetailViewController. When tap on a table cell, it navigate to DetailViewController. In DetailViewController, I want to edit and reload the FirstViewController's table view

How can I use NSNotification for this problem?

Here's the method I want to implement NSNotification stuff

-(IBAction) save{
strSelectedText=theTextField.text;

[NSNotificationCenter defaultCenter];
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];  

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



[self.navigationController popViewControllerAnimated:YES];
}

Upvotes: 6

Views: 11413

Answers (2)

priyanka
priyanka

Reputation: 2076

-(void)viewDidLoad {

[NSNotificationCenter defaultCenter];
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];  

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

}


-(IBAction) save{

[[NSNotificationCenter defaultCenter] postNotificationName:MyNotification object:sender];

//this will go to where you implement your selector objFirstViewController.

}

-(void)objFirstViewController:(NSNotification *)notification {

}

Upvotes: 9

Ved
Ved

Reputation: 612

post the notification from detailViewController and add firstViewController as the observer.

Make sure you remove fireViewController from the observer list from viewDidUnload.

Right now you are adding detailViewController as observer.

Upvotes: 0

Related Questions