我如何使用Swift编程以编程方式转换到不同故事板中呈现的不同视图控制器?

3
let customerStoryboard =   UIStoryboard(name: "Customer", bundle: nil) //First(Current) storyboard 
let agentStoryboard =   UIStoryboard(name: "Agent", bundle: nil)  //Second storyboard

var otherVC = UIViewController!   // ViewController reference

otherVC = agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile") 

最后一条语句没有执行或显示结果,是否需要做其他的事情?
3个回答

1
你只需要实例化视图控制器。要展示它,你需要使用presentViewController方法。
someVC.presentViewController(otherVC, animated: true, completion: nil)

1
使用此代码来显示第二个视图控制器。
let otherVC : AnyObject! = self.agentStoryboard.instantiateViewControllerWithIdentifier("AgentProfile")
self.showViewController(otherVC as UIViewController, sender: otherVC)

请参考以下问题以获取更多信息:https://dev59.com/HGAf5IYBdhLWcg3wu0tD - Julian E.
我可以使用上述相同的代码在同一个故事板中切换视图控制器吗? - Anand Yadav
@ Julian E.--- 在第二个参数中,如何让otherVC成为发送方? - Anand Yadav
发送者必须是目标视图控制器。您可以查阅此SO答案以获取更多信息:https://dev59.com/HGAf5IYBdhLWcg3wu0tD - Julian E.
showViewController(otherVC as UIViewController, sender: otherVC) 会隐藏我的导航栏在下一个视图控制器中,有没有什么方法可以避免这种情况? - Anand Yadav

0
let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController

self.navigationController.pushViewController(secondViewController, animated: true)

@ RKP,使用上述代码时我遇到了以下错误-----致命错误:在解包可选值时意外地发现了nil。 - Anand Yadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接