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