使用Swift切换故事板

28

我在Xcode项目中有一个故事板,它变得太大了,导致我的电脑速度变慢。如何通过编程(或手动使用storyboarding)从当前故事板中的一个视图使用按钮前进到新故事板上的一个视图?

我对Xcode还不太熟悉,所以越简单越好。谢谢!


你有多少个视图控制器?最好解决一下为什么会有这么多视图控制器的原因。 - Ian
我有大约20个视图控制器,因为我的代码中有很多数学计算,不想用更多的代码来复杂化视图。 - Seth
不要忘记设置你的“取消返回连线(unwind segues)”。你可以在这里阅读更多相关信息:使用取消返回连线 - damianesteban
请查看此链接:http://robsprogramknowledge.blogspot.pt/2012/06/linking-storyboard.html - Jakub Vano
3个回答

49
你可以通过编程的方式来实现它: Swift 3+
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerID") as UIViewController
present(vc, animated: true, completion: nil)

更年长的

let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("nextViewController") as UIViewController
presentViewController(vc, animated: true, completion: nil) 

在Sort中,您可以这样做:

presentViewController( UIStoryboard(name: "myStoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("nextViewController") as UIViewController, animated: true, completion: nil)

不要忘记给您的下一个视图控制器添加ID。

有关更多信息,请参见此处


41

0 行代码

使用界面生成器

  1. 向源storyboard添加Storyboard引用
    故事板引用
  2. Storyboard引用控制拖动到UIButton
    在此输入图像说明
  3. Storyboard引用占位符的属性检查器中,指定目标UIViewController的目标Storyboard和可选的目标Reference IDBundle.
    在此输入图像说明

完整图片

在此输入图像说明


1
太棒了,你正在使用故事板对象进行解释!非常有用 - mavesonzini
这太棒了。非常感谢。 - Yucel Bayram
1
你是从哪里了解到这个的?太棒了! - Nupur Sharma

5

Swift 3 解决方案:

present( UIStoryboard(name: "CreateOrder", bundle: nil).instantiateViewController(withIdentifier: "firstOrderViewController") as UIViewController, animated: true, completion: nil)

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