ios - 在父 UIViewController 中收到多个 NSNotifications

标签 ios swift nsnotificationcenter nsnotifications

我有一个用于某些 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/

相关文章:

ios - 检测 iOS 应用程序中的日期变化

ios - Uibutton titlelabel shadowoffset 属性在 ios 7 中的行为不正常

ios - 使用 swift 从默认系统图标创建 UIImage

c# - Xamarin NSNotificatioCenter : How can I get the NSObject being passed?

ios - 从后台线程更新 UI 的最佳/最常见方式

objective-c - 合并来自后台线程的更新后,NSFetchedResultsController 未触发委托(delegate)方法

ios - 嵌套 ScrollView 与 ios 中的 TableView

swift - IAd 横幅将整个 skscene 向上移动

ios - 如何覆盖/删除应用程序中保存的图像

swift - 在后台 Swift 中跟踪推送通知