我将通知从UITabBarController中的第一个viewController发送到UITabBarController中的第二个viewController,但第二个viewController似乎不会观察或侦听通知,直到我打开it....so基本上我必须打开第二个viewController才能订阅通知返回第一个viewController发送it....How是否可以修复此问题,以便通知到达secondViewController而不必打开它进行订阅
发布于 2017-12-19 20:06:42
在SecondViewController中实现通知接收功能
@objc func notified(_ noti : Notification) {
print("Recieved fired noti")
}为您的UITabbarController创建一个自定义类。在Tabbarcontroller类的viewDidLoad中,编写以下代码行
let vc = self.viewControllers![1] as! SecondViewController
NotificationCenter.default.addObserver(vc, selector: #selector(vc.notified(_:)), name: NSNotification.Name(rawValue: "NotificationSample"), object: nil)这里我仅以索引1为例,将其替换为tabbarController中的secondViewController索引。
然后从你想要的任何地方发布你的通知,例如,从FirstviewController
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationSample"), object: nil)在最好的情况下,这应该是可行的。
https://stackoverflow.com/questions/47885896
复制相似问题