从另一个视图控制器中删除通知观察者 [英] Remove notification observer from another view controller

查看:46
本文介绍了从另一个视图控制器中删除通知观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Iphone 应用程序,其中正在为 UIApplicationWillEnterForegroundNotification 注册一个通知观察者.现在我想从另一个视图控制器中删除那个.我的代码看起来像

Hi I am developing Iphone application in which am registering one notification observer for UIApplicationWillEnterForegroundNotification. Now I want to remove that one from another view controller. My code looks like

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(applicationBecomeActive)
 name:UIApplicationWillEnterForegroundNotification
 object:nil]; 

我正在创建一种删除观察者的方法:

And I am creating one method for removing observer:

-(void) removeObserver
{
  [[NSNotificationCenter defaultCenter] removeObserver:self]; 
} 

我正在从其他视图控制器调用此方法,但它不起作用.我想我必须存储观察者.但我不知道如何做到这一点.需要帮忙.谢谢.

and I am calling this method from other view controller but its not working. I think I have to store observer. But I don't know how to do this. Need help. Thank you.

推荐答案

第二个视图控制器需要引用第一个视图控制器的实例.让我们假设它保存在一个属性中:

The second view controller needs a reference to the instance of the first view controller. Let's assume it is hold in a property:

@property (nonatomic, strong) FirstViewControler *firstViewController;

然后,您作为观察者移除第一个视图控制器的代码如下所示:

Then your code to remove the fist view controller as an observer would look like this:

- (void)removeObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.firstViewController]; 
}

缺少的部分是:您必须在某处设置属性.在何处执行此操作取决于您的代码.

The missing part is: You have to set the property somewhere. Where to do that depends on your code.

这篇关于从另一个视图控制器中删除通知观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆