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

问题描述 投票:1回答:3

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

  1. 导航到屏幕(深层链接),所以我必须从rootviewcontroller导航到几个屏幕。
  2. 无论用户在应用程序中的哪个位置,都可以导航到rootviewcontroller。

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

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

  • 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)
}
ios swift uinavigationcontroller apple-push-notifications rootview
3个回答
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)

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
                }
            })

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

© www.soinside.com 2019 - 2024. All rights reserved.