简体   繁体   English

如何快速使用代码移动到现有的UIViewController(在情节提要中)

[英]How to move to an existing UIViewController (in storyboard) with code in swift

I know how to do it with buttons by clicking and dragging, but I have a situation where I need a transition without a button. 我知道如何通过单击和拖动来使用按钮来完成此操作,但是我遇到一种情况,我需要没有按钮的过渡。 so what code will allow me to transition from the current UIViewController to a different UIViewController that is already initialized? 那么什么代码将允许我从当前的UIViewController过渡到已经初始化的另一个UIViewController?

First, you will need to create the segue in the storyboard and give a proper name to the identifier of this segue. 首先,您需要在情节提要中创建序列,并为该序列的标识符指定适当的名称。 You can do this by clicking on the segue (left panel) and then click on Attributes (right panel). 您可以通过单击segue(左侧面板),然后单击Attributes(右侧面板)来执行此操作。

在此处输入图片说明

The same way you can call this segue using buttons or selection of table rows from your storyboard, you can call it from code using performSegueWithIdentifier:sender: 使用按钮或从情节提要中选择表行来调用此segue的方式相同,也可以使用performSegueWithIdentifier:sender:从代码中调用它performSegueWithIdentifier:sender:

As when using the button and cells on uitableview, your view controller call prepareForSegue: You override this method in your view if you need to pass any data between controllers: 与在uitableview上使用按钮和单元格一样,视图控制器调用prepareForSegue:如果需要在控制器之间传递任何数据,则可以在视图中覆盖此方法:

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        if segue!.identifier == "MySegue" {
            let viewController:ViewController = segue!.destinationViewController as ViewController
            viewController.receiveSomedata = self.sendSomeData
        }
}
  1. Drag a segue from viewController to viewController 将一个segue从viewController拖到viewController

  1. Set identifier for this segue 设置此segue的标识符

  1. When you want to go to next viewController 当您想转到下一个viewController时

      self.performSegueWithIdentifier("yoursegue", sender: nil) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UIViewcontroller(StoryBoard)内部向上移动模式UIView - How to move up a modal UIView inside of a UIViewcontroller (StoryBoard) 如何为没有故事板的 UIViewController (Swift) 提供标识符? - How to give an identifier for UIViewController (Swift) which has no storyboard? 如何在不使用Swift中的Storyboard的情况下以编程方式加载UIViewController - How to load a UIViewController programmatically without using Storyboard in Swift Swift:如何移动在情节提要中添加的按钮位置 - Swift: How to move button position that added in Storyboard 如何使用情节提要segue将UIViewController连接到UIView,而无需任何代码? - How to connect UIViewController to UIView using storyboard segue, without any code? 无法在 Swift Package 中显示 UIViewController 及其 Storyboard - Not able to present UIViewController with its Storyboard in a Swift Package 在新的Storyboard中将值传递给UIViewController - Swift - Passing Values to UIViewController in new Storyboard - Swift Swift 中 UIViewController 的自定义初始化,在 storyboard 中设置接口 - Custom init for UIViewController in Swift with interface setup in storyboard 在故事板上使用Swift嵌套类作为UIViewController - Use Swift nested class as UIViewController in storyboard 将情节提要添加到现有代码 - Adding a storyboard to existing code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM