在另一个ViewController中删除AppDelegate的观察者

时间:2014-11-11 08:59:25

标签: ios object nsnotificationcenter

NSNotification观察者在didFinishLaunchingWithOptions

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newShipCome:) name:kNewShipNotifaction object:nil];

我想在另一个ViewController中删除观察者。

我在我的一个ViewController

中完成了这个
[[NSNotificationCenter defaultCenter] removeObserver:[UIApplication sharedApplication] name:kNewShipNotifaction object:nil];

但仍无效。有谁知道怎么做?

感谢

1 个答案:

答案 0 :(得分:3)

你需要删除AppDelegate的观察者,你在removeObserver:方法中传递了错误的对象。

而不是:

[[NSNotificationCenter defaultCenter] removeObserver:[UIApplication sharedApplication] name:kNewShipNotifaction object:nil];

使用:

[[NSNotificationCenter defaultCenter] removeObserver:[[UIApplication sharedApplication] delegate] name:kNewShipNotifaction object:nil];
相关问题