ios - 使用通知执行 segue 后 ViewController deinit

标签 ios swift notifications nsnotificationcenter

我基本上收到了远程通知,我想在用户单击通知后立即将其重定向到正确的 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/

相关文章:

swift - 从 UIStackView 中拖动和删除 UIView 但将其保留在其放置目的地

android - 如何在没有上下文的情况下显示弹出消息

ios - iPad 未出现在 Storyboard 中的 "view as"部分

ios - UILocalNotification 用户信息中的 Swift 字典

ios - 如何正确地水平和垂直对齐两个按钮

ios - 更新表格 View 内容框架原点

javascript - setInterval 这是获取通知的正确方法吗?

html - Webkit 通知 requestPermission 函数不起作用

ios - Swift中的额外参数调用

ios - 调整 UIPopoverController 的大小