使用addObserverForName时删除Observer:usingBlock [英] Remove Observer when using addObserverForName:usingBlock

查看:167
本文介绍了使用addObserverForName时删除Observer:usingBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在加载视图时添加一个观察者。

I have the following code that adds an observer in the loading of the view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserverForName:@"com.app.livedata.jsonupdated"
                                                      object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) {
                                                          NSLog(@"JSONUPDATED");
                                                      }];
}

这就好了。但是当卸载视图并确认调用dealloc时,Notification仍在触发。

And this fires fine. However when the view is unloaded and I confirm the dealloc is called the Notification is still firing.

似乎没有停用此观察者的方法?

There doesn't seem to be a method for deactivating this observer?

推荐答案

似乎解决方案是在视图中跟踪对象,然后您可以在dealloc方法中引用它。

Seems the solution is to track the object in the View and then you can reference it in the dealloc methods.

 id observer = [[NSNotificationCenter defaultCenter] addObserverForName: /* ... */ ];

然后删除如下:

[[NSNotificationCenter defaultCenter] removeObserver:observer];
observer = nil;

这篇关于使用addObserverForName时删除Observer:usingBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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