在我的应用程序中,有两个viewControllers为FirstViewController
和DetailViewController
。
点击表格单元格时,会导航到DetailViewController
。在DetailViewController
中,我想编辑并重新加载FirstViewController
的表格视图
如何使用NSNotification
解决此问题?
以下是我想要实现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];
}
答案 0 :(得分:9)
-(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 {
}
答案 1 :(得分:0)
发布detailViewController的通知,并添加firstViewController作为观察者。
确保从viewDidUnload中的观察者列表中删除fireViewController。
现在你将detailViewController添加为观察者。