包含 UIViewController 流的 UIViewController

UIViewController containing a flow of UIViewControllers

我正在构建一个包含另一个 UIViewController内部 viewController)。为此,我使用容器视图。现在我希望内部 viewController 推到另一个 UIViewController,所以我基本上希望内部 viewController 成为 UINavigationController 的子级。我知道容器视图一旦初始化就不能更改它的内容(反正不能直接更改),所以我碰壁了。

有没有人对我如何将 UINavigationController 嵌套在 UIViewController 中有任何想法,或者我是否需要重新考虑我解决问题的方法?

你可以简单地拿一个UINavigationController。 用你的 InnerViewController.

初始化它

现在,将 UINavigationController 添加为 (childViewController + addSubview + didMoveToParentViewController) 在 OuterViewController 中。

现在 InnerViewController 位于 UINavigationController 内。你可以在 UINavigationController.

上推送你想要的任何内容

深受@7vikram7 的启发,我最终得到了一个容器视图,其中嵌入了一个 UINavigationController,它以我的 InnerViewController 作为根 viewController:

在我的 InnerViewController 中,我现在可以推送到任何 UIViewController,并控制我的导航堆栈,例如:

override func viewDidLoad() {
    super.viewDidLoad()

    // Hide the navigation bar
    self.navigationController?.setNavigationBarHidden(true, animated: false)

    // Push to whatever viewController I want to
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("myAwesomeViewController") as! AwesomeViewController
    self.navigationController?.pushViewController(viewController, animated: true)
}