如何使用NSNotification

时间:2011-08-23 12:53:40

标签: iphone objective-c ios ios4

在我的应用程序中,有两个viewControllers为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];
}

2 个答案:

答案 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添加为观察者。