【发布时间】:2016-06-06 13:31:54
【问题描述】:
我有一个父 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(但出现时不要删除)?
【问题讨论】:
标签: ios swift nsnotificationcenter nsnotifications