我了解 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/