通过appDelegate openURL函数呈现viewController(在Navigation层次结构中)

sha*_*ses 2 uinavigationcontroller appdelegate swift

我的初始视图控制器是一个登录页面,它检查当前用户是否已登录.如果是,则对下一个视图执行segue,这是一个标签栏控制器,其默认选择的选项卡是一个表视图(具有已嵌入导航控制器).

在此表视图中是一个事件对象列表.当用户选择事件对象时,事件详细信息将显示在新视图中,导航栏将显示在上方,允许用户返回其事件列表.这一切都正常.

但是,该应用程序有一个URL方案,允许它通过URL启动.在我的app Delegate中,我能够捕获我需要的URL方案和查询.

我的问题是当我尝试呈现必要的视图控制器时.我实际上能够呈现视图控制器(这是一个事件详细信息视图控制器),但是这个视图控制器不在我需要的导航层次结构中.无法弄清楚如何做到这一点(下面的代码再次在appDelegate的openURL函数中,通过我的自定义URL方案调用):

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

        //the viewController to present
        let viewController = storyboard.instantiateViewControllerWithIdentifier("SingleEventAfterCreationViewController") as! SingleEventAfterCreationViewController

        let eventListNavController = storyboard.instantiateViewControllerWithIdentifier("EventsListNavigationController") as! UINavigationController

//set a bunch of properties on my viewController

//below is where I'm unsure. The correct view controller is presented, but the navigation bar above, and tab bar below is lost. Subsequently, when I click on a "user" of the event, the program crashes, I'm guessing because there is no navigation hierarchy. 

self.window?.rootViewController = eventListNavController

                self.window?.rootViewController?.presentViewController(viewController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

崩溃文本:

由于未捕获的异常'NSGenericException'而终止应用程序,原因是:'无法找到segue的导航控制器'showProfileSegue'.Push segues只能在源控制器由UINavigationController实例管理时使用.

我附上了我的故事板的图像,以帮助说明我的问题,以及现在通过URL打开应用程序时发生的事情的图像,以及该视图中的哪些操作崩溃了.我还附上了呈现的视图控制器应该是什么样子的图像.

故事板全屏:https://s3.postimg.org/oywzb595f/storyboard_help.png

我的故事板: 故事板

问题: 我现在在哪里

应该是什么样的: 应该是什么样子

sha*_*ses 6

我想到了.

最初我在我的故事板中设置我的初始视图控制器,我删除了.我现在正在AppDelegate的didFinishLoadingWithOptions函数中以编程方式设置它.

接下来,在我的app delegate函数中,我这样做了:

let rootTabBarController = self.window?.rootViewController as! UITabBarController

let firstNavigationController = storyboard.instantiateViewControllerWithIdentifier("EventsListNavigationController") as! UINavigationController

rootTabBarController.viewControllers![0] = firstNavigationController

//the viewController to present
let viewController = storyboard.instantiateViewControllerWithIdentifier("SingleEventAfterCreationViewController") as! SingleEventAfterCreationViewController

//set variables in viewController

firstNavigationController.pushViewController(viewController, animated: true)
Run Code Online (Sandbox Code Playgroud)

当用户尚未登录应用程序时,我可能遇到问题(由于我通过AppDelegate设置的初始viewController),但我的基本问题已经解决.