简体   繁体   中英

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?

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).

在此处输入图片说明

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:

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:

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

  1. Set identifier for this segue

  1. When you want to go to next viewController

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM