[英]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.