image from: surfiran.com

Traveling through the App: the Segue magic! — Part 1

Jahid Hasan Polash
InfancyIT
Published in
4 min readMar 3, 2018

--

The real voyage of discovery consists not in seeking new landscapes, but in having new eyes. - Marcel Proust

Navigating through the pages in an iOS app can be performed in various ways. I prefer the two simplest ways in practice: Instantiate ViewControllers with Storyboard identifiers and more preferably using Segues. I always find Segue more interesting and magical. Plus it has lots of features and customizations. Today I will try to focus a little light on Segues and how to work with them.

First of all, Segue is an object that binds two ViewControllers. Major functionality is to instantiate destination view controller and bring it to focus hiding source view controller. Now the terminology “Source ViewController and Destination ViewController” is simply to say you go from the source view controller to destination view controller when you trigger the segue in the source view controller. Seems tough? let’s see,

image from: Apple Developers Documentations

The left one is the Source view controller and the right one is the destination view controller and the segue connects them. When you trigger the segue from the left view controller, it immediately instantiates the right view controller and brings it up on the screen. Easy huh? Creating a segue is easy too. Keeping Control pressed click from the component of the source view controller and drag and release on the destination VC.

image from: Apple Developers Documentations

Now at this point you will be shown some options to select on how the segue will be presented.

image from: Apple Developers Documentations

These options are presentation and action types through how the segue will perform. Describing these options need a lot of talking, thus we will do that another day. Let’s go ahead.

Now, if you place the segue on any component of the source VC, the action of that component will automatically trigger the segue. Such as,

Here the segue is placed on the BarButton. Thus when the button will be pressed the segue will be triggered automatically. To manually trigger a segue we should call

performSegue(withIdentifier: String, sender: Any?)

method. Here the identifier is a string that should be set in the storyboard. Click on the segue and in the Attribute inspector set the identifier.

You can call performSegue from anywhere of your controller. Now before actually perform the transition, segue will call a special method prepare(for segue: UIStoryboardSegue, sender: Any?) which gives you a place to set some values or we can say transfer data to the destination VC. The implementation can be as,

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toNextVC" {
let vc = segue.destination as! SecondViewController
vc.someVariable = someValue
}
}

While the method is called, segue already has instantiated the destination VC and let the user access the VC from the source VC as segue.destination. Here we can also implement the callback functions (we will discuss it another time).

There is a common parameter named sender in both performSegue(withIdentifier: String, sender: Any?) and prepare(for segue: UIStoryboardSegue, sender: Any?) functions. We can send any parameter in sender from performSegue function which we can catch in the prepareForSegue function. Especially working with tableViewCell actions or collectionViewCell actions this particular parameter is a life saver. i.e.

performSegue(withIdentifier: "toNextView", sender: value)override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toNextVC" {
let vc = segue.destination as! SecondViewController
vc.someVariable = sender as? valueType
}
}

Here I have discussed the very basics of UIStoryboardSegue. Of course, there are tons to know more about them. On the second part, I will discuss the Unwind Segue and where to use them. Also, how Unwind segue saved my app. 😄

If you like this article, please help me some claps. 😛 Happy coding.

--

--