ViewController 使用 Notification 执行 segue 后 deinit
ViewController deinit after performing segue using Notification
我基本上是在收到远程通知,我想在用户单击通知后立即将他重定向到正确的 VC。
我正在使用 NSNotificationCenter 从我的根 VC 执行 segue,引导用户到正确的 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 没有被初始化,直接去初始化。我不确定为什么会这样,也许我没有正确移除观察者。
有什么建议吗?
如果您正在接收远程通知,我建议您只在 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
}
我基本上是在收到远程通知,我想在用户单击通知后立即将他重定向到正确的 VC。
我正在使用 NSNotificationCenter 从我的根 VC 执行 segue,引导用户到正确的 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 没有被初始化,直接去初始化。我不确定为什么会这样,也许我没有正确移除观察者。
有什么建议吗?
如果您正在接收远程通知,我建议您只在 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
}