【问题标题】:How to use NSNotification如何使用 NSNotification
【发布时间】:2011-08-23 12:53:40
【问题描述】:

在我的应用程序中有两个 viewController,分别是 FirstViewController 和 DetailViewController。 当点击表格单元格时,它会导航到DetailViewController。在DetailViewController,我想编辑并重新加载FirstViewController的表格视图

如何使用NSNotification 解决这个问题?

这是我要实现的方法NSNotification stuff

-(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];
}

【问题讨论】:

标签: iphone objective-c ios ios4


【解决方案1】:
-(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 {

}

【讨论】:

    【解决方案2】:

    从 detailViewController 发布通知并添加 firstViewController 作为观察者。

    确保从 viewDidUnload 的观察者列表中移除 fireViewController。

    现在您正在添加 detailViewController 作为观察者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多