UIViewController上没有以编程方式创建的segue

时间:2017-05-30 08:53:16

标签: ios swift uinavigationcontroller segue

我在VC上A,我以编程方式转到vc B,然后VC B没有转到VC Cperform(segueWithIdentifier:)崩溃)。 但是如果我使用segue转到VC B,我可以调用segue转到VC C

这是我崩溃的代码:

VC A

// Method A (that does not crash)
// self.performSegue(withIdentifier: "gotoB", sender: self)

// Method B (that crashes)
let dst = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "vcB")
if let navigator = navigationController {
    navigator.pushViewController(vc, animated: true)
}

VC B

self.performSegue(withIdentifier: "gotoC", sender: self) // crashes here

问题是我希望能够从任何地方到达VC B,因此使用导航控制器进行编程推送。我想了解为什么会发生这种情况以及如何实现我的目标。感谢。

1 个答案:

答案 0 :(得分:1)

如果您想从任何地方访问此VC,您必须以编程方式从故事板中实例化它然后呈现/推送它。像这样:

   let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: self))

   let viewController = storyboard.instantiateViewController(withIdentifier: "YourViewControllerId")

   navigationController.push(viewController, animated: true)

确保在故事板中为此视图控制器设置StoryboardID