我基本上收到了远程通知,我想在用户单击通知后立即将其重定向到正确的 VC。
我使用 NSNotificationCenter 从我的 rootVC 执行转场,引导用户到达正确的 VC。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "chooseCorrectVC:", name:chatNotificationKey, object: nil)
由于观察者之前已加载,因此首先调用我的 chooseCorrectVC 函数,因此这是我的“Init/Deinit”日志。每当 viewDidLoad() 被调用时我都会考虑 Init。
rootVC INIT
SecondVC DEINIT
rootVC DEINIT
func chooseCorrectVC(notification:NSNotification){
self.performSegueWithIdentifier("chatSegue", sender: notification)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
问题是:用 chatSegue 调用的 VC 不会被初始化并直接进入 deinit。我不确定为什么会发生这种情况,也许我没有正确删除观察者。
有什么建议吗?
最佳答案
如果您收到远程通知,我建议您仅在 AppDelegate.swift 的方法中处理通知:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
// Here you can define view controller and manage it.
println("Received: \(userInfo)")
// Make root view controller first as per your need as HomeViewController in following
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var mainTabBarController = mainStoryboard.instantiateViewControllerWithIdentifier("MainTabBarController") as! MainTabBarController
var notificationNavController : UINavigationController = mainTabBarController.viewControllers?.first as! UINavigationController
var homeViewController : HomeViewController = notificationNavController.viewControllers.first as! HomeViewController
homeViewController.isNotified = true
let nav = UINavigationController(rootViewController: mainTabBarController)
self.window!.rootViewController = nav
nav.setNavigationBarHidden(true, animated: false)
self.window?.makeKeyAndVisible()
}
您可以通过在此处设置标志来管理将另一个 View Controller 推送到 homeviewcontroller 的 viewdidload 上。查看时已加载
if isNotified == true {
// Push another view controller
}
关于ios - 使用通知执行 segue 后 ViewController deinit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30740206/