How to use NSNotification

In my application, there are two viewControllers like FirstViewController and DetailViewController . When you click on a table cell, go to DetailViewController . In DetailViewController I want to edit and reload the view of the FirstViewController table

How can I use NSNotification for this problem?

Here is 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]; } 
+6
source share
2 answers
 -(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 { } 
+9
source

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

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

Now you add detailViewController as an observer.

0
source

Source: https://habr.com/ru/post/895673/


All Articles