通过 appDelegate openURL 函数呈现 viewController(在导航层次结构中)
Presenting viewController (within Navigation hierarchy) via appDelegate openURL function
我的初始视图控制器是一个登录页面,它检查当前用户是否已登录。如果是,则对下一个视图执行 segue,这是一个选项卡栏控制器,其默认选择的选项卡是table 视图(已嵌入导航控制器)。
在此 table 视图中是一个事件对象列表。当用户选择事件对象时,事件详细信息会显示在新视图中,导航栏会出现在上方,允许用户 return 返回到他们的事件列表。这一切都正常工作。
但是,该应用程序有一个 URL 方案,允许它通过 URL 启动。在我的应用程序委托中,我能够捕获我需要的 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)
崩溃文本:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'showProfileSegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
我附上了我的故事板图像以帮助说明我的问题,以及通过 URL 打开应用程序时现在发生的情况的图像,以及该视图中的什么操作导致它崩溃。我还附上了呈现的视图控制器应该是什么样子的图像。
故事板全屏:https://s3.postimg.org/oywzb595f/storyboard_help.png
我的故事板:

问题:

它应该是什么样子:

我想通了。
最初我是在故事板中设置初始视图控制器,但我已将其删除。我现在在 AppDelegate 的 didFinishLoadingWithOptions 函数中以编程方式设置它。
接下来,在我的应用委托函数中,我这样做了:
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)
我可能 运行 在用户尚未登录应用程序时遇到问题(由于我通过 AppDelegate 设置的初始 viewController),但我的基本问题已经解决.
我的初始视图控制器是一个登录页面,它检查当前用户是否已登录。如果是,则对下一个视图执行 segue,这是一个选项卡栏控制器,其默认选择的选项卡是table 视图(已嵌入导航控制器)。
在此 table 视图中是一个事件对象列表。当用户选择事件对象时,事件详细信息会显示在新视图中,导航栏会出现在上方,允许用户 return 返回到他们的事件列表。这一切都正常工作。
但是,该应用程序有一个 URL 方案,允许它通过 URL 启动。在我的应用程序委托中,我能够捕获我需要的 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)
崩溃文本:
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'showProfileSegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
我附上了我的故事板图像以帮助说明我的问题,以及通过 URL 打开应用程序时现在发生的情况的图像,以及该视图中的什么操作导致它崩溃。我还附上了呈现的视图控制器应该是什么样子的图像。
故事板全屏:https://s3.postimg.org/oywzb595f/storyboard_help.png
我的故事板:
问题:
它应该是什么样子:
我想通了。
最初我是在故事板中设置初始视图控制器,但我已将其删除。我现在在 AppDelegate 的 didFinishLoadingWithOptions 函数中以编程方式设置它。
接下来,在我的应用委托函数中,我这样做了:
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)
我可能 运行 在用户尚未登录应用程序时遇到问题(由于我通过 AppDelegate 设置的初始 viewController),但我的基本问题已经解决.