简体   繁体   English

如何使用NSNotification

[英]How to use NSNotification

In my application there is two viewControllers as FirstViewController and DetailViewController . 在我的应用程序中,有两个viewControllers作为FirstViewControllerDetailViewController When tap on a table cell, it navigate to DetailViewController . 点击表格单元格时,它会导航到DetailViewController In DetailViewController , I want to edit and reload the FirstViewController 's table view DetailViewController ,我想编辑并重新加载FirstViewController的表视图

How can I use NSNotification for this problem? 如何使用NSNotification解决此问题?

Here's the method I want to implement NSNotification stuff 这是我想要实现NSNotification的方法

-(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];
}
-(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 {

}

post the notification from detailViewController and add firstViewController as the observer. 从detailViewController发布通知并添加firstViewController作为观察者。

Make sure you remove fireViewController from the observer list from viewDidUnload. 确保从viewDidUnload中的观察者列表中删除fireViewController。

Right now you are adding detailViewController as observer. 现在您将detailViewController添加为观察者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM