Swift didReceiveRemoteNotification - 无论app用户位于何处,都可以导航到rootviewcontroller(现在有错误信息)

时间:2015-02-23 11:50:23

标签: ios swift uinavigationcontroller apple-push-notifications rootview

在IOS Swift应用程序中收到推送通知后,我想根据通知中的内容做两件事:

  1. 导航到屏幕(深层链接),所以我必须从rootviewcontroller导航到几个屏幕。

  2. 导航到rootviewcontroller,无论用户位于应用中的哪个位置。

  3. 第二个我认为是第一个的先决条件。

    我知道我需要在这两个函数中放置代码:

    • didReceiveRemoteNotification
    • didReceiveLocalNotification

    错误消息:'UIViewcontroller?'没有名为'navigationController'的成员

    在文件AppDelegate.swift中:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 
    {
        println("didReceiveRemoteNotification")
        //Navigate to rootviewcontroller
        var rootViewController = self.window!.rootViewController
        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("CurrentShows")
            as ViewController
    
        //rootViewController.navigationController?
        //    .popToViewController(setViewController, animated: false)
    }
    

3 个答案:

答案 0 :(得分:0)

尝试此代码在您的方法中:

var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var setViewController = mainStoryboard
    .instantiateViewControllerWithIdentifier("CurrentShows")
        as ViewController_CurrentShows

rootViewController
    .navigationController
    .popToViewController(setViewController, animated: false)

答案 1 :(得分:0)

需要创建变量: var window:UIWindow

以下代码在appdelegate中实施,位于: func didBecomeActive()....

let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("Login") as Login_ViewController

            self.window?.rootViewController?.dismissViewControllerAnimated(false, completion:
            {
                if self.window != nil
                {
                    self.window!.rootViewController = rootController
                }
            })

答案 2 :(得分:0)

我编写了一个简单的类,通过传递类类型,从一行代码中的任何位置导航到视图层次结构中的任何视图控制器,因此您编写的代码也将与视图层次结构本身分离,例如:

Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()

..或者您也可以在主线程上异步执行方法:

Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
    MyViewControllerInstance?.doSomethingAsync()
}

这里是GitHub项目链接:https://github.com/oblq/Navigator