Switching Views Programmatically in Swift

泪湿孤枕 提交于 2019-11-27 14:13:00

You can add a segue between the two scenes in your storyboard by control dragging from the view controller icon at the top of the first scene to the second scene:

You can then select that segue and give it a unique storyboard identifier, and then you can just perform the segue using that identifier:

performSegueWithIdentifier("SegueToSecond", sender: self)

Alternatively, you can do something like:

let controller = storyboard?.instantiateViewControllerWithIdentifier("Second") as SecondViewController
presentViewController(controller, animated: true, completion: nil)

where "Second" is a storyboard identifier I specified for that destination scene in Interface Builder.


By the way, there are alternatives if you're creating your destination scene programmatically or using NIBs, but storyboards are more common, so I focused on that in my examples above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!