Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 932
smartsanja Avatar asked Aug 23 '11 12:08

smartsanja


People also ask

What is Nsnotification Center in Swift?

A notification dispatch mechanism that enables the broadcast of information to registered observers. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+

How do I use NotificationCenter?

Let's see how to use it - Based on the key NotificationCenter can observe. NotificationCenter. default — This is the notification variable you can create it globally inside your class if you having more notifications. addObserver(self, — This is for the class where we are going to observer notification.

How do I add alerts to observer?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()


1 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 {

}
like image 85
2 revs, 2 users 66% Avatar answered Oct 24 '22 03:10

2 revs, 2 users 66%