【问题标题】:Unable to navigate to specific UIViewController when handling local notification action (when app is in background)处理本地通知操作时(当应用程序在后台时)无法导航到特定的 UIViewController
【发布时间】:2015-08-26 09:09:29
【问题描述】:

我正在尝试实现用户接收本地通知的流程,然后在将他们带到特定视图控制器(而不是根)时响应特定操作。

我的理解是我需要推送到 rootViewController,然后从那里推送到所需的destinationViewController。

这是我在 AppDelegate 通知处理程序中最成功的尝试:

//  self.window?.rootViewController = root
//  self.window?.makeKeyAndVisible()

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("DestinationViewController") as! DestinationViewController
self.window?.rootViewController?.navigationController?.popToViewController(viewController, animated: false)

'popToViewController' 行什么都不做。事实上,唯一发生的事情是当我取消注释两条注释行时,但是一旦我将 ViewController 设为根并使其可见,它就会失去与 NavigationController 的连接。这让我觉得 NavigationController 是实际的根,但我无法将其设置为 rootViewController。

我应该补充一点,我已经在 Storyboard 中创建了 NavigationController。我应该以编程方式创建它吗? 我做错了什么?

提前致谢。

编辑 我也在忙着尝试这个,感觉它可能会走在正确的道路上,但仍然没有成功:

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var rootVC = mainStoryboard.instantiateViewControllerWithIdentifier("DashboardVC") as! DashboardVC
let nav = UINavigationController(rootViewController: rootVC)
self.window?.rootViewController = nav

let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("DestinationViewController") as! DestinationViewController 
nav.popToViewController(viewController, animated: false)

【问题讨论】:

    标签: ios swift uilocalnotification


    【解决方案1】:

    所以我不确定这是否是最好的方法,但它适用于应用程序是否在后台运行以及是否关闭:

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var root = mainStoryboard.instantiateViewControllerWithIdentifier("DashboardVC") as! DashboardVC
    let nav = UINavigationController(rootViewController: root)
    
    self.window?.rootViewController = nav
    
    root.performSegueWithIdentifier("destinationSegue", sender: self)
    

    我选择了 performSegueWithIdentifier 路线,因为“​​popToViewController”方法都没有做任何事情。

    【讨论】:

    • 这些行应该写在哪里?
    猜你喜欢
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多