­

[Swift] What is segue? baby don't hurt me

By 10:35

Segue [pronounced seg-way] is great to have and use in Swift.
They allow you to send information from one page to another in the application. And with unwind segues you're able to send data back to the previous page.
In this post i'll be explaining how to send data back and forth with  the help of segues.

Let's assume we have 2 view controllers: A and B
and let's say we want to send data over from view controller A to B.
The following Swift code in view controller A prepares the sending of data:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
}

So in this piece of code, you will want code the piece of data you want to send over to view controller B. But first you want to make sure there is a segue connection from A to B.



So what I did here, was I made a view controller A that makes a segue connection whenever you click on the button. You'll want to go for a push segue and after making this connection a new line will appear:


Whenever you click on this new connection, you can go to the Attributes Inspector where you can give an ID to your segue. This ID will be used to identify your segue connection in the code, which is nice to have whenever you have multiple segues happening. With these two view controllers I also have two Cocoa Touch Class connected where I can have the code for the segues:


So let's go see the code now.


The code in the prepareForSegue function checks what segue.identifier is being called when you push the button. Then you should assign the destinationViewController. 
The code in the B controller for receiving the object from A is as follows:

And the result after pushing the button:

Tadah! You just created your first segue!

0 reacties