Advanced iOS Features
The course is part of this learning path
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.
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 we can connect this first ViewController with the second ViewController, so that we can go back and forth anytime we want. So, before we do that, what defines which ViewController is shown first. So, you see this little arrow on the first ViewController.
So, this defines the entry point of our app. So, if I run my app right now I will see the ViewController. First ViewController because this arrow is here. So, this is first ViewController as we expected.
If I take this arrow and put it in the second ViewController, if I run this, right now I am going to see the second ViewController. So, that's how you change your entry point. But I don't want to do this, I want to really start with the first ViewController and then find a way to go to second ViewController anytime I want. But bear in mind that you can change anytime you want, you can drag this entry point to any ViewController.
So, there are a couple of ways to execute Segways. Remember Segway, we have talked about this, this is basically the connection between the first ViewController and second ViewController. So, let me create a button over here and we're going to make it that, so when a user taps on this button it will go to second ViewController. And I am going to make it in a way that I am just going to do it from here, I am not even going to use a code. I am just going to create the Segway in the main storyboard and this will be some stating methods.
This will be not a living thing. When a user taps on this, it will just take them to the second ViewController. In order to do that, hit 'Control' and drag the first button to second ViewController like this, and drop. As you can see, it creates a Segway. So, we have to choose Segway type over here. We generally go for a show and we have some others like present modally. And in most of the cases, you don't want present modally or you don't want the other action Segways. Over here the show is very useful when it comes to going back and forth, and you're going to see how we can leverage this.
So, if you choose it, it will create this arrow of that you see, and this is the Segway itself. If you click on the Segway, you can see it attributes as a regular view. And in the right hand side you see that it has an identifier if you want to give it one. It doesn't have much really, only has an identifier. And we're going to need an identifier in order to differentiate Segways because we're going to see more than one.
So, I have just connected this button to the second ViewController and I didn't do anything else. If I click on this, as you can see, it takes me to the second ViewController in a way that it pop ups like this and the user can just take it down anytime they want. So, this is cool. This is very good, this is animated, this is practical. But most of the time you don't see this kind of Segway in apps. Most of the time you see a back button, in order to go back and forth between ViewControllers. So, choose your ViewController and I am going to show you how to do that.
Go to Editor and say Embed In Navigation Controller. So, this will bring in a new controller for us and we're not going to see that controller in the real app. As you can see, it seems like a new ViewController, but it's actually not. It just gives us a navigation controller and navigation bar inside of our apps. Now, as you can see, we have created a navigation bar in the first ViewController and whenever we Segway from first ViewController to another one, the another one will get navigation bar automatically with a back button.
So, this is very, very practical. And this is what we see inside of apps generally. So, let me run this. I'll show you how it works. And you'll love this because it's so practical, it's so easy. If I click on this, here you go, I am in the second. If I click back, I am in the first. So, this is kind of cool. And that's basically it. But there will be sometimes that you don't want to just assign a Segway to a button. Maybe you want to do something before you go and show information on the second ViewController. For example, if I had to get a name from user and display it in the second ViewController, I cannot do that without writing code.
So, it's not a good way to assign a Segway from a button to a ViewController. This is the easiest way, but we generally don't prefer it. We prefer it with code.
So, let me create this kind of setup for you so you will understand what I am saying in a better way. So, let me drag in a text field. So, within this text field, we're going to get a name from user or just a text from user, and we'll try to display this text in the second ViewController. So, I am going to create another button over here and I am not going to assign a new Segway to this button.
So, let me go to placeholder and say name. So, user will be giving his/her name in here, and in this button I am going to say next. So, let me make this a little bit bigger and make it central. Okay. So, here we go, we have what we need. And again, I am not going to go and just do say grating this time. Rather I am going to place a label over here and this will display the name. So, this is going to be name, nameLabel. And we're going to display what we get in this first ViewController.
And this is kind of hard because we cannot reach the variables inside of one ViewController from another one if we don't know what we're doing. Because they are completely different swift files. So, let me first try and assign this into our code as a variable. So, let me drag and drop and call this nameLabel or nameText. So, this is nameText and I am going to need this button. I have not even assigned the other button as an action.
Remember, we didn't write any code to show and run this Segway. Right now we need one because I am not going to do as I did before. So, let me choose second ViewController because I am going to define this nameLabel here as well, and let me say nameLabel. So, that's basically it. We have defined our objects into coding section. Now we're going to stop here and continue building this Segway app in the next lecture.
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.