[英]How to use NSNotification
在我的应用程序中,有两个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];
}
-(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 {
}
从detailViewController发布通知并添加firstViewController作为观察者。
确保从viewDidUnload中的观察者列表中删除fireViewController。
现在您将detailViewController添加为观察者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.