简体   繁体   English

iOS 通知收到:有的可以,有的不可以

[英]iOS notification receive: some can and the other can't

I use a UINavigationController to control the view controller change.Supposing I push A1 and A2 one by one.我使用UINavigationController来控制视图 controller 的变化。假设我将 A1 和 A2 一个一个地推。 Both A1 and A2 is the instance of my custom View Controller A. A has registered a notification, and it will refresh the UI when it receive the notification. A1和A2都是我自定义View Controller A的实例,A注册了通知,收到通知会刷新UI。

My problem is:我的问题是:
when I post the notification in A2, and A2 can receive it.当我在A2发布通知,A2可以收到。
However, when I pop to A1, there is no change in A1.但是,当我弹到A1时,A1没有任何变化。

So could any one help me how to fix it?那么任何人都可以帮助我如何解决它吗?

BTW,the reason why I use notification is that I may push several instances of A, and if any one posts the notification, the other instances in the stack also need to update.顺便说一句,我之所以使用通知是因为我可能会推送多个 A 实例,如果任何一个发布通知,堆栈中的其他实例也需要更新。

Just a guess, but只是一个猜测,但是

Are you registering for the notifications in viewDidLoad: or viewDidAppear:?您是否在 viewDidLoad: 或 viewDidAppear: 中注册通知? Are you unregistering in viewDidUnload: or viewDidDisappear:?您是在 viewDidUnload: 还是 viewDidDisappear: 中注销?

Normally, using the appear/disappear methods to register for notifications is the best choice, since a view controller will only be interested in a notification while it's visible.通常,使用 appear/disappear 方法注册通知是最佳选择,因为视图 controller 只会对可见的通知感兴趣。

Whichever approach you use, though, make sure that your register and unregister calls are in matching UIViewController methods.但是,无论您使用哪种方法,请确保您的注册和取消注册调用匹配 UIViewController 方法。

Following is your problem - My problem is: when I post the notification in A2, and A2 can receive it.以下是你的问题 - 我的问题是:当我在A2中发布通知时,A2可以收到它。 However, when I pop to A1, there is no change in A1.但是,当我弹到A1时,A1没有任何变化。

When you pop to A1, then at that time A2 is deallocated and notification/listener that you have created there is also removed, so it won't respond to notification.当你弹出到 A1 时,那时 A2 被释放,你在那里创建的通知/侦听器也被删除,所以它不会响应通知。 Now if you want A1 also to listen notification then you need to add observer in A1 as well.现在,如果您希望 A1 也监听通知,那么您还需要在 A1 中添加观察者。

It's strange it can receive the notifications after I restart the Xcode(4.3.1 (4E1019)).奇怪的是,我重启Xcode(4.3.1 (4E1019)) 后还能收到通知。

thanks for your guys' help.感谢你们的帮助。

BTW, the object will receive the notification if it is not deallocated.顺便说一句,如果 object 没有被释放,它会收到通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM