如何从 UIViewController 过渡到 UINavigationController

How to transition to a UINavigationController from a UIViewController

我正在尝试通过 segue 从 UIViewController 过渡(模态)到 UINavigationController。 我的 UIViewControllerUINavigationController 在界面生成器中。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    self.performSegueWithIdentifier("showNavController", sender: self)
}

但是它一直给我警告: Warning: Attempt to present <UINavigationController: 0x14d5095e0> on <Test.ViewController: 0x14d60d9d0> whose view is not in the window hierarchy!

您需要为您的导航控制器定义一个根视图关系,否则它没有显示。

A navigation controller is a container view controller—that is, it embeds the content of other view controllers inside of itself.

Every navigation stack must have at least one view controller to act as the root.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html

为什么要在 prepareForSegue 中调用 performSegueWithIdentifier
这就是导致错误的原因。您不必再次调用 performSegueWithIdentifier

performSegueWithIdentifier用于执行segue操作。 prepareForSegue 在您调用 performSegueWithIdentifier 函数之后和 segue 转换发生之前被调用。