In the previous lessons, we worked out the BMI value. In the finished app, 1
this value should be displayed on a separate screen when the calculate button gets pressed. 2
So now that we've got the BMI value worked out, how can we navigate to a new screen and pass this result 3
over to be displayed? 4
Notice how our screen with the sliders is linked to a ViewController.swift file. 5
So our second screen also needs its own view controller file. 6
So how do we create such a new file? 7
Well, firstly we're going to right-click on the Controllers folder and we're going to create a New File inside 8
this folder and the type of file we're going to choose is a Swift File, and then we're going to name 9
this file 10
SecondViewController. 11
So notice how we've currently got a ViewController.swift which gets created every time we build 12
a new iOS project and it comes with some template code already written for us. 13
Well, in this case, we've created a second ViewController.swift file entirely from scratch. And the 14
first thing that I'm gonna do is I'm going to try and create what we have over here which is a class. 15
And the class that I'm going to create, instead of being a view controller which controls our first screen, 16
this calculator screen, I'm going to create a class called SecondViewController. 17
So as soon as I start typing the word "class," you'll notice that the code snippet comes in. And if I hit 18
enter, it'll insert all of the place holders that I need to fill. 19
So what's the name of my class gonna be? Now, in Swift, 20
it's convention to name the file that contains the class the same name as the class itself. 21
So my class is actually gonna be called SecondViewController and the SuperClass is going to be the 22
same as we've got over here in our first screen that Apple created for us. 23
It's going to be a UIViewController. 24
Now, 25
notice that as I'm typing this, I'm getting some errors here. And the error tells me that I'm using an 26
undeclared type 'UIViewController." so it doesn't know what data type this is. 27
But how come we got to use it here? 28
Well, it's because we were importing something called UIKit. And here, we're simply importing Foundation. 29
These are what we call frameworks. 30
These are bunches of code that Apple has written to make our experience of developing iOS apps much 31
easier than it would be if we had to build everything from scratch. 32
Now, Foundation allows you to work with most the features of the Swift programming language. But if you 33
want anything that is specific to iOS, say, a UILabel, or a UISlider, or, in our case, a UIViewController, 34
everything basically that starts with a UI comes from the UIKit Framework. 35
If you go in to Help and go to Developer Documentation, here you'll find on the left here a section on 36
UIKit. 37
So this is everything that the framework contains. And you can read more about it here as well as see 38
all of the things that it includes. 39
And it's really, really broad-ranging, everything from the UIController to other things that we've been 40
working with such as the UILabel or the UIViewController. 41
And this is what we need to be able to manage our second screen. 42
So as soon as we go over here and we switch out foundation for UIKits, because UIKit actually already 43
includes everything that's in Foundation. It's a level higher and it already has the foundational capabilities, 44
but it has a lot more. So now that we've imported this UIKit Framework, 45
well, we now have access to all of the components that Apple has already built inside UIKit, including 46
for example, a UILabel or a UIColor, or maybe the UIImageView. 47
So these are already things that we've come across and we've used. These are the components that Apple 48
has pre-built, so that we can simply drop it into our code to start using it without having to define 49
that. 50
Labels have text properties. They also have a background color property or a text color property. 51
All of these things have already been done for us. So if we take a look at this new class that we've 52
created, 53
notice how this code looks really similar to how we created struct. 54
Well, similar to struct, we start off by using a keyword. Instead of struct, 55
it's now a class. 56
And then, we defined the name of our class which is also capitalized begin with. 57
And these names which start off with a capital letter are differentiated from the ones which don't, like 58
the variable names 59
and the function names, so that we know that when we use these, we're tapping into a blueprint, a Struct 60
blueprint, or in this case, a Class blueprint. 61
The other thing that normally gets created for us when we create a new iOS project and we get our default 62
ViewController.swift is the viewDidLoad method. 63
But in this case, because we're creating everything ourselve, let's create the viewDidLoad method from 64
scratch. 65
So as soon as I start writing viewDidLoad, 66
you can see that there is the suggested viewDidLoad method. aAd if I hit enter, it'll insert all the 67
code that's required. 68
Now, after I delete this code place holder, you can see that this looks much the same as what we've got 69
in our template ViewController. 70
The only thing that's missing is this super.viewDidLoad. 71
So let's add that to our viewDidLoad as well. 72
Now, that we've successfully created our own ViewController from scratch, given how important of a component 73
the ViewController is in iOS, 74
I really recommend taking a quick look at the documentation for the UIViewController to start to understand 75
what it does. 76
And in this lesson, as we create our own ViewController from scratch, it'll help you understand a lot 77
of things that are covered. Feel free to go either via Developer Documentation in Xcode if you have 78
it downloaded, or simply just go to developer.apple.com/documentation, and use the search 79
bar as we do normally.