1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Advanced iOS Features: Permanent Features, Segue, Alert Message, Timers, and Gestures

Information Transfer


Advanced iOS Features
Storing Data
PREVIEW11m 37s
Second Screen
14m 39s

The course is part of this learning path

Start course
1h 57m

This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.


To the most out of this course, you should have some basic understanding of programming and computer science in general.



Hi. Within this lecture, we're going to see how to perform segues with inside of code. We're not going to assign a static segue right now. We're just going to do the segue inside of our ViewController.swift. And in order to do that, we're going to create the segue from here.

Choose this yellow part. It means the View Controller and just drag and drop 2nd ViewController and choose Show Segue. And this will create a segue between these apps. And the second one comes from the button but the first one comes from the view controller itself. So, it's not assigned to any view right now. So, I'm going to give this an identifier. I'm going to say, toSecondVC, because we will need this identifier because we're going to call this segue inside of our code.

For example, if you come over here and say performsegue to your nextClicked, and this should be performsegue. Like this. So, double click on that or hit Tap. It will ask you for an identifier and I'm not looking for this shouldPerform or canPerform. I'm looking for performSegue. So in here, I'm going to give the identifier that I have written so it has to be exactly the same. So, toSecondVC, toSecondVC. And sender, it means that this is a helper object that can transmit messages. We're going to do that manually. So, I'm just going to say nil. So, this will perform the segue that I have created with this identifier.

So, I have written this inside of my nextClicked. So, I can write this anywhere I want. It doesn't have to be button. It can be anything I want. I can write this inside of a function. I can write this inside of a button. As you can see now, when I click next, it takes me to 2nd ViewController. So right now, all I have to do when I type a name here, like Atil, hit Next. I want it to show it here. But in order to do that, we need to learn about some new stuff as well because in the 2nd ViewController, I have some variables but I cannot reach them in the 1st ViewController. In fact, let me create a new variable here and assign it to be my name label.text because we're going to need that eventually.

So, we have to do it under class but outside of my, we have did lot function. So, I'm not defining this into a function. I'm defining this outside of my functions, inside of my class so that we can reach it from everywhere. So, I can assign it to be myName. So, this is okay. Now, only thing to do is just to connect this myName with View Controller. So, we have this variable over here but I cannot just come here and call this. I cannot just say myName from here because it won't be in the 1st ViewController, it will be in the 2nd ViewController. By the way, do product build or command B when you create a second view controller, so every file on your app should be synchronized. So after you do that, we're going to learn about a new function called Prepare For Segue. 

So within this function, we tell what to do right before we do our segue. So if you write prepare, you will see prepare(for segue and this is an override function. It means that this is generated for us before and we are overriding. We're editing this function and we can just say if this segue is coming from this view to that view, do this. Of course, we can have multiple segues in an app and we have identifiers to differentiate them. So, we can use these identifiers in here.

So before we do that, let me create the username variable and I'm going to say nameText.text and it's actually okay to force on raptors because I'm sure that this text exists and it's going to take some empty string, at least. So, what I basically want is to make this username = myName in the 2nd ViewController. So, we can use prepare(for segue : function in order to do that because this gives us a segue. This gives us a UI storyboard segue and we can use this in order to understand where we are going and where we're going from. So, how do you do this? You control.

You start by controlling segue identifier and this segue refers the given segue in this function. You can check to see if the segue has really the second VC identifier. If that's the case, we're going to do. what we have to do. So, what we want to do is actually defined the 2nd ViewController. So, let me show you in the main storyboard, you can understand it better. So, we have some view controllers and we have some segues in the storyboard and the segue will have a destination. So, it's coming from this view controller to 2nd view controller. And 2nd ViewController is the destination right now. So if I say, segue.destination, it will give me the view controller that I'm going to. So, we can use this. So, let me say, segue.destination. And as you can see it gives me a UIViewController and I can cast this view controller to be my second view controller and I can use this as a variable.

So, let me create this variable. This is let destination VC and of course, I'm going to use segue.destination. And this will give me the view controller, but this is going to be a UIVewController and I'm going to cast this as my second view controller. Because I know I'm going to the second view controller because I've checked the segue identifier. So, I'm going to use as as before, and I'm going to first cast this. I'm pretty certain this is going to be my second view controller. If you have any doubts over here, you have to differentiate your segues. You have to make sure that this is definitely going to be SecondViewController or else this will fail. This will crash.

So now, if I say destinationVC, as you can see, we have SecondViewController as a variable and we can reach whatever we want. We can reach myName string from here. So right now, I've connected my SecondViewController as a variable and I have managed to reach the attributes, the variables of that view controller. So, I can just make it = username. And of course, in order to do that, I have to come over here and do this insides of my class but outside of my functions. So, I'm going to initialize this as an empty string. And rather than saying var here, I'm just going to say userName is = userNameText.text!. Why I'm doing this inside of next clicked rather than we did load because I want to assign this name text dot text when actually user clicks on nextClicked, not when the app is loaded.

So, we're getting this error here so, let me try and do command B. This error was nothing so if I do command B, it will go away. So, here we go. We have what we need so, let me run this and let me try to give a name to my text field and hit Next. So let me say, Atil and hit Next. And let me go to 2nd first. If I hit this, as you can see, I don't see anything and if I hit Next, now I see Atil here. So as you can see, this works in a way that we want. We are controlling the segue identifier. If that is correct, we are casting our second view controller as a variable so that we can reach it anytime we want. We're doing that inside of nextClicked, so when I click the nextClicked, it worked. 

But when I clicked the second button in which I don't have any code. it didn't take my name and just show it on the second view controller because it only works if the segue identifier is actually to toSecondVC. So, that's how you work with segues. That's how you work with second screens. You can write so much more in this view controller and in fact, we're going to build a lot of apps during this course and we're going to use segues, most of them. So, we're going to be practicing this a lot in the upcoming lectures. So, we can stop now and within the next lecture, we're going to continue using this app but rather than segues, I'm going to show you some other concept called View Controller Life Cycle.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics