如何使用NSNotification

在我的应用程序中有两个viewController作为FirstViewControllerDetailViewController 。 当点击表格单元格时,它将导航到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作为观察者。