ios - 在 AppDelegate 之外,我如何在基本上 `performSegueWIthIdentifier` 之前在 UINavigationController 中包含一个 UIViewController?

标签 ios swift uiviewcontroller uinavigationcontroller segue

我了解 AppDelegate 中的概念。

基础是这样的:

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

var mainViewController = mainStoryboard.instantiateViewControllerWithIdentifier(
“MainViewController”) as! UIViewController

var navController = UINavigationController(rootViewController: mainViewController)

self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()

这将成功地使您的 mainViewController 成为 NavigationController 并像这样运行。

我不想在 AppDelegate 中这样做。

我有一个名为 A 的 UIViewController,它包含在 UINavigationController 中。从 A,我现在做的是我 performSegueWithIdentifier 到另一个名为 B 的 UIViewController。我想以编程方式使 B 包含在 UINavigationController 中。我不想在 Storyboard 中实现这一点。这必须以编程方式完成。

我想我必须使用 mainStoryboard.instantiateViewControllerWithIdentifier(...) 来实例化 B,然后将它包装在 A 某处的 UINavigationController 中(最好是我计划转到 B 的地方)。然后,我可以通过 performSegueWithIdentifier 以外的方式到达 B 吗?因为如果我执行 segue,那么它不会转到我包装在 UINavigationController 中的 B。

我希望这不会太复杂,但如果有任何不合理的地方,我可以提供详细信息。

最佳答案

除了实际显示新的 View Controller 之外,您可以执行此操作的一种方法类似于您在代码示例中使用的方法。假设你在 View Controller A 中有一个方法应该显示 View Controller B,你可以这样做:

func showViewControllerB() {
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let viewControllerB = mainStoryboard.instantiateViewControllerWithIdentifier("ViewControllerB") as! ViewControllerB
    let navController = UINavigationController(rootViewController: viewControllerB)
    presentViewController(navController, animated: true, completion: nil)
}

关于ios - 在 AppDelegate 之外,我如何在基本上 `performSegueWIthIdentifier` 之前在 UINavigationController 中包含一个 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32213750/

相关文章:

ios - UIApplicationDidBecomeActiveNotification 和 viewWillAppear 导致数据源冲突

ios - 是应用程序 :didFinishLaunchingWithOptions: really a good place to initialize cocos2d and its OpenGL ES view?

ios - 如何调试 Ionic iOS 滑动手势问题

arrays - 使用元组和 3 个 UIPickerview 计算小计

ios - 如何使用 swift 为多注释设置数组

objective-c - 在哪里设置弹出窗口中显示的 View Controller 的大小?

ios - 嵌入 UIViewController 的子容器的自动布局

android - Cordova - 如何没有启动画面?

ios - 不确定如何修复 NSInconsistencyException