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 work with more than one screen. So far, we have been working on single screen apps. We haven't added new ViewControllers. We haven't added new screens to our apps. And in this lecture we're going to see how to do that. And this is basically going to show you how to create new ViewControllers, how to create new Swift files, and connect them together, so you can add as many as you want when you build an app. And we're going to need that information in the upcoming apps of course.
So, choose single view app. And I am going to name this SegueApp. So, you're going to see what a Segue is but it's basically connection between ViewControllers in your Swift projects. So, I am not going to check any Swift UI or Core Data over here and I am going to hit 'Next' and I am going to place this under iOS Complete. And we're just going to go into main storyboards in order to add our new ViewControllers. So, let me go to main storyboard over here. And we're going to see what we usually see when we create a project, an empty iPhone screen. So, let me zoom out a little bit. So, this is our ViewController. And this is our storyboard.
We can have as many as we want in here. As many as screens we want and we can actually order these screens; the first will be this, second will be this, third will be this. We're just creating the blueprint of what we're building in here. So, can I add a label over here and just make this a little bit bigger because I want to call this first screen or first ViewController. Because we're going to be working with more than one. So, I am just going to differentiate ViewControllers using labels. So, this is first ViewController. And let me make the central. That's great. So, I am just going to need another one to call second ViewController. And how do we add a ViewController over here? How do we add a second screen? It's fairly easy. We're going to use library one more time and we're going to look for ViewController. So, you can just drag and drop this as a view. So, here we go. This is ViewController that we are looking for. Just drag and drop here. So, here it is, our second screen, second ViewController. So, I can just drag and drop whatever I want to, in the screen as well. I can use library and as you can see in the left hand side, I have a different view hierarchy right now.
So, if I drag at label, as you can see, I can drag and drop it, and I can just design it in a way that I want exactly like I am doing in the first ViewController. So, let me name the second ViewController. And that's okay. So, here we go. We have first ViewController. We have second ViewController. Now, when I click on this, this yellow dot over here in the ViewController, it means that I am choosing the ViewController itself. And let me add another assistant editor over here and choose ViewController from here. And let me go to my first ViewController and try to drag and drop this view. And as you can see, I can do it. I can call this my label and connect it.
So, can I connect my second ViewController label to here. So, let's try. Let's see it control and try to drop it. As you can see, we cannot do that, because this view over here, this ViewController is not connected to my ViewController.swift. So, this controller is actually connected to this Swift file. And the second one is not connected. So, we can create a new Swift file and assign it to be connected to this ViewController. And how do we do that? If you click on this yellow dot one more time, you can drag and drop anytime you want by the way to align these ViewControllers and it doesn't mean anything. But it's good to be aligning this. So, let me create the ViewController file first. So, let me go to File, New File. And here, I am not going to go for Swift file. I am going to go for Cocoa Touch Class. So, we haven't seen them yet. This is the first time you're seeing it and I am going to explain what it is.
So, basically a Cocoa Touch Class is the way to go when you try to create something like ViewController. If you click over here, you will see a lot of view types. So, UI View Controller, Table View Cell, we're going to see what are those later on. But if I want to create a view, every related file, you need to go with the Cocoa Touch Class and you need to choose what kind of view you're trying to create, what kind of class you're trying to create from here. And after that, of course you can choose whatever name you want.
Just make sure that your subclass is UIViewController. And make sure Swift is chosen and for Class Name we already have a ViewController. So, I am going to name the SecondViewController. So, that's it. I am going to create, hit 'Next' to create this and it will just make sure that this target is selected and it will just put this inside of my project folder. As you can see, now we have another ViewController file. So, we have another Swift file and we can use the Swift file with our newly created ViewController. So, let me delete this comment lines and let me go to main storyboard. Because right now I've created this, but does Xcode know
this is going to be connected to this file right now. Of course not. I have created this but I didn't connect it. So far, maybe I have created five Cocoa Touch Classes and five ViewControllers. Xcode couldn't know which one is which. So, I need to manually connect.
So, again this is UIViewController, as you can see, because we have chosen it to be when we create a Cocoa Touch Class. So, let's connect it. Let's use this yellow dot one more time, choose ViewController. It has to be ViewController itself. So, make sure you touch this yellow button and here I am going to go for this Identity Inspector rather than the Attributes Inspector. So, if you go to this Identity Inspector, you will see something called Class. So, if you click this dropdown button over here, you'll find the SecondViewController that we have created. So, make sure this is selected and make sure when you click on it one more time, you'll see SecondViewController.
Now, this view is connected to the ViewController file that we have created. So, let me test this. Let me go to the SecondViewController.swift and let me go to SecondViewController main storyboard, and drag and drop. As you can see now, I can actually create my label here. So, you can call it my label one more time, or you can call it whatever you want because these are completely different Swift files. You can just use the same names or you can just use completely different names. It doesn't matter. So, that's how you create new views. But we don't know yet how to connect these two together. How to reach second view from the first view. That's what we're going to do within 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.