Ios ViewController在使用通知执行segue后取消初始化

Ios ViewController在使用通知执行segue后取消初始化,ios,swift,notifications,nsnotificationcenter,Ios,Swift,Notifications,Nsnotificationcenter,我基本上收到了一个远程通知,我想在用户单击通知后立即将其重定向到正确的VC 我正在使用NSNotificationCenter从我的rootVC执行一个序列,引导用户找到正确的VC NSNotificationCenter.defaultCenter().addObserver(self,选择器: chooseCorrectVC:,名称:chatNotificationKey,对象:nil) 由于之前加载了观察器,因此首先调用我的chooseCorrectVC函数,因此这是我的“Init/Dei

我基本上收到了一个远程通知,我想在用户单击通知后立即将其重定向到正确的VC

我正在使用NSNotificationCenter从我的rootVC执行一个序列,引导用户找到正确的VC

NSNotificationCenter.defaultCenter().addObserver(self,选择器: chooseCorrectVC:,名称:chatNotificationKey,对象:nil)

由于之前加载了观察器,因此首先调用我的chooseCorrectVC函数,因此这是我的“Init/Deinit”日志。每当调用VisualDead Load()时,我考虑init。

rootVC初始化

二次脱硝

脱硝剂

问题是:用chatSegue调用的VC不会被初始化,而是直接进入deinit。我不知道为什么会发生这种情况,也许我没有正确地移除观察者


有什么建议吗?

如果您收到远程通知,我建议您只需通过AppDelegate.swift at方法处理通知:

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() 
}
通过在此处设置标志,您可以管理要在homeviewcontroller的viewdidload上推送的另一个视图控制器。在视图上没有加载

if isNotified == true {
      // Push another view controller
}

我正在为我的第一个VC做这件事(这是一个嵌入在导航视图控制器中的VC)。您将如何执行到第二个VC的转换?我就是这样做的。让navigationController=UIStoryboard(名称:“Main”,bundle:NSBundle.mainBundle())。实例化eviewControllerWithiIdentifier(“InitialController”)为!如果let window=window,则UINavigationController{window.rootViewController=navigationController@ArvindI已编辑了您自定义问题的答案。我建议在这种情况下不要使用通知中心。您可以与我共享代码以进一步检查您的问题。它成功了!非常感谢Arvind。我仍然想知道它为什么不与通知一起工作。有什么想法吗?不确定请先检查代码,但在设置rootviewcontroller之后,可能会以错误的顺序执行notification observer。在调试断点时,这可能会起作用。
if isNotified == true {
      // Push another view controller
}