我有一个用于某些 View 的父级 UIViewController
BaseViewController
:
class TestViewController: BaseViewController { ...
我想在 BaseViewController
中接收通知,所以我在其 ViewWillAppear
中添加了一个观察者:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(BaseViewController.receivedNotification(_:)), name:"NotificationIdentifier", object: nil)
我无法删除 ViewWillDisappear
上的观察者,因为通知是从呈现的 UIViewController
发送的,它可以呈现在每个 UIViewController
上,父 BaseViewController
所以 BaseViewController
总是消失。
所以在浏览应用程序后,有不止一个观察者添加,我多次收到通知。
如何只执行一次通知选择器?或者如何在推送另一个 UIViewController
时 .removeObserver
(但在出现时不删除)?
最佳答案
你可以很好地使用:
NSNotificationCenter.defaultCenter().removeObserver(observer: <The class from which you want to remove>, name: "Name of notification which you want to remove", object: nil)
关于ios - 在父 UIViewController 中收到多个 NSNotifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37658853/