标签栏(视图控制器)中调用多次ios的通知中心方法方法?如何移除观察者? [英] Notification center method method called multiple time ios in Tab bar (View controller)? How to remove observer?

查看:53
本文介绍了标签栏(视图控制器)中调用多次ios的通知中心方法方法?如何移除观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发布通知表单一个视图控制器,它位于选项卡栏应用程序的导航堆栈中.我正在删除 dealloc as 中的观察者.

I am posting notification form one view controller which is in navigation stack in tab bar application. I am removing observer in dealloc as.

 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ArtworkAddedReloadAllArtworks" object:nil];  in dealloc correctly.

我注销了

[[self tabBarController] dismissViewControllerAnimated:YES completion:nil];

但问题是当我重新登录时,在通知中心添加了两次相同的通知

but issue is when i login back same notification added twice as in Notification center

ArtworkAddedReloadAllArtworks, 0x12ff698, 0x7a490e00, 1400
ArtworkAddedReloadAllArtworks, 0x12ff698, 0x7a431800, 1400

在通知中心创建了 2 个视图控制器实例,通知添加了两次不同的对象,我如何取消注册通知?我无法从 viewDidDisappear 中删除,因为我在登录后从其他控制器发布通知.dealloc 正在调用.

In Notification center as 2 instance of view controller is created and notification added twice two different objects, How do i unregister notification ? i cant remove from viewDidDisappear as i posting notification from other controller.dealloc is calling after login.

如何制作 nil 视图控制器(登录前先创建)和他的观察者?

How to make nil view controller(created first before login),and his observer?

提前致谢.

推荐答案

如果发布类和观察类不同,我无法在观察类中删除观察者.

If posting class and observing class different then, i can't remove observer in Observing class.

例如.在 A 类中添加了称为观察者的 viewWillAppear,现在我推送到 B 类,然后调用 A 类的 viewWillDisappear 并且观察者移除现在通知不会被 A 类观察,因为它在推送操作时在 viewWillDisappear 中被移除.

Ex. In Class A viewWillAppear called observer added, now i push to Class B then Class A 's viewWillDisappear called and observer removed now notification won't observe by Class A as it's removed in viewWillDisappear while push operation.

我认为这不是正确的解决方案,但我最终通过保存观察者类引用并用它来删除引用.

I think this is not proper solution but i end up by saving observer class reference and used that to remove reference.

viewDidLoad 方法中的 A 类(观察类)

Class A (Observing Class) in viewDidLoad method

  [[ASNGlobalClass shareManager] setNotificationSelf:self];//Save reference of self

B 类(任何类)

[[NSNotificationCenter defaultCenter] removeObserver:[[ASNGlobalClass shareManager] notificationSelf]
                                                    name:@"LookbookChangeNotification"
                                                  object:nil];

这篇关于标签栏(视图控制器)中调用多次ios的通知中心方法方法?如何移除观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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