SWIFT4 — All you need to know about using Segues!

Fahd Saifuddin
4 min readDec 30, 2017

--

Note:Updated Xcode 9.2, iOS 11, and Swift 4.

Today I am going to talk to you about segues. A clear understanding of how to use segues forms the building blocks of a developers approach to developing apps in IOS. Your apps would be very flat if all they had was a single screen and no transition. Even if somehow you could do the best possible workarounds to fit everything your business needs in a single screen, the boiler plate code behind it will render the whole implementation error prone and useless — let aside your role as an IOS developer!

In the ever changing IT world the assumption of this article todate will be that you are using the latest IOS 11 and Xcode 9.2, released recently.

Theoretical understanding: In the real world the word segue means to go from one place to another- an uninterrupted form of transition from point A to point B.

Segue(segway) in real world!

In the Apple world segues are a means of moving from one screen-a ‘View controller’ to another ‘View controller’. Screen transitions can be done both programatically or with the use of segues — and a sometimes using a combination of both. Segues can be triggered based on a user clicking a button, a value returned from REST APIs(or endpoints) like a successful login or based on a user selection on the user interface you want to transition him to another form. In a nutshell segues are the basic building blocks in IOS navigation.

Enough theory, lets open Xcode and get our hands into the mud!

Step 1: Open Xcode. Using the Object explorer find and drag a new view controller to your main.storyboard on the Interface builder.Your screen should look something like the one below on completion.

Drag and Drop a View Controller from the Object finder in your Main.Storyboard

Step 2: Next, drag and drop a button object onto your first view controller.

Step 2: Drag and drop a button object on the first View Controller on Main.storyboard

Step 3: By keeping your button selected, and pressing the control key from your mac, drag and drop to the second view controller.

Step 3: Drag and drop from first VC to second VC

When you release the mouse, a drop down menu appears on the second View controller. Here you have the following options: Show, Show Detail, Present Modally, Present as Popover and Custom.

Show: also called a Push segue. This type of segue allows you to navigate through a stack of view controllers, on top of each other. A good use of this type of navigation is when we are embedding a navigation view controller(more on that later on a separate tutorial). For now just understand that pushes the destination view controller, from right to left on the stack. Example usage could be navigating like into folders or email inboxes.

Show detail: used mainly when you are working with split view controllers.

Present modally: Shows View controllers with animations.

Custom Segue:You may implement your own custom segue and have control over the behaviour.

The deprecated segues were deprecated in IOS 8, so I will not cover them for this tutorial.

Step 4: when you have successfully connected your first VC with the second VC, your screen should look something like the one below.

Main.storyboard after successfully connecting the button with the second ViewController

Step 5: That’s it. This was super easy! Give the button a meaningful title, by either double clicking it or using the properties explorer and changing the title property, You can also change the background colour of the second ViewController to something else other then the default white, so you see the tranistion happening.

Gooosh! The engineering team at Apple made this such a cake for the IOS developer. Hit play and see the transition happen in action.This was a basic introduction to a novice user. In my next tutorial, I will cover more in-depth on segues to take you on the next level. Keep on the lookout for more.

If you have any questions or comments, leave me a note here or email me on fahdsaif@hotmail.com.

--

--

Fahd Saifuddin

Blockchain Project Manager and Technologist — A technical management person who knows how to talk less and deliver more.