簡體   English   中英

使用Notification執行Segue后,ViewController取消初始化

[英]ViewController deinit after performing segue using Notification

我基本上收到了一個遠程通知,我想在用戶單擊通知后立即將其重定向到正確的VC。

我正在使用NSNotificationCenter從rootVC執行segue,將用戶引導到正確的VC。

NSNotificationCenter.defaultCenter()。addObserver(自身,選擇器:“ chooseCorrectVC:”,名稱:chatNotificationKey,對象: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沒有初始化,而是直接去初始化。 我不確定為什么會這樣,也許我沒有正確刪除觀察者。

有什么建議么?

如果您收到遠程通知,建議您僅通過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() 
}

您可以在此處設置標志來管理另一個視圖控制器,以將其推送到homeviewcontroller的viewdidload上。 在視圖上確實加載了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM