0 1 00:00:00,980 --> 00:00:06,870 So the first thing we're gonna do is, of course, clone our starting project which you can find in the 1 2 00:00:06,870 --> 00:00:08,590 course resources link. 2 3 00:00:08,700 --> 00:00:14,160 And for those of you guys who want practice creating the User Interface and doing the Auto Layout, then 3 4 00:00:14,160 --> 00:00:17,380 there's, of course, the alternative that you can work on. 4 5 00:00:17,460 --> 00:00:22,920 In other words, you've got two stops to choose from, one stub with the screen already designed for you, 5 6 00:00:22,950 --> 00:00:28,560 so you can focus on Swift programming and one where you can practice recreating this design on your 6 7 00:00:28,560 --> 00:00:29,370 own. 7 8 00:00:29,370 --> 00:00:35,400 Now, the goal of this lesson is the setup our project and link up our storyboard to the view controller. 8 9 00:00:35,430 --> 00:00:40,760 That way we can make our buttons interactive and show different questions on the screen. 9 10 00:00:40,950 --> 00:00:43,250 So here's our Main.storyboard. 10 11 00:00:43,380 --> 00:00:49,870 And the first thing we have in our stack is a label, and then we have a True button and a False button. 11 12 00:00:49,950 --> 00:00:57,540 And finally, at the very bottom, we've got a progress view which is styled as a Progress View bar. 12 13 00:00:57,540 --> 00:01:05,540 So now all we have to do is open up the assistant editor and link up our code with our design. 13 14 00:01:05,580 --> 00:01:12,600 So firstly, I'm going to create an IBOutlet from my Question label as well as my progress view, and I'm 14 15 00:01:12,600 --> 00:01:21,120 going to call the top one questionLabel, and I'm going to call my progress view, simply a progressBar. 15 16 00:01:22,370 --> 00:01:22,660 All right. 16 17 00:01:22,680 --> 00:01:27,780 So we now have our two IBOutlets, our questionLabel and our progressBar. 17 18 00:01:28,290 --> 00:01:33,240 I'm going to create two more IBOutlets for each of the buttons. 18 19 00:01:33,240 --> 00:01:42,120 So one from the True button which I'm just gonna call trueButton and then another one from my False 19 20 00:01:42,120 --> 00:01:46,830 button which is going to be called falseButton. 20 21 00:01:46,860 --> 00:01:47,130 All right. 21 22 00:01:47,160 --> 00:01:50,010 So everything on screen now has an outlet. 22 23 00:01:50,010 --> 00:01:57,870 So the final thing to do is to add an IBAction which gets triggered when either the True or the False 23 24 00:01:57,900 --> 00:02:00,360 button gets pressed. 24 25 00:02:00,360 --> 00:02:02,350 So let's start from the true button. 25 26 00:02:02,400 --> 00:02:10,430 And I'm going to call this action answerButtonPressed and, of course, I'm going to change the type of 26 27 00:02:10,490 --> 00:02:15,730 my sender to a UI button, and then hit Connect. 27 28 00:02:15,830 --> 00:02:22,700 And now I'm going to click back and drag it back to the False button as well so that they're both connected. 28 29 00:02:22,700 --> 00:02:22,940 All right. 29 30 00:02:22,970 --> 00:02:28,070 So now we're done with all of our linking, so we can head straight into the ViewController.swift 30 31 00:02:28,460 --> 00:02:30,410 and see what we have done. 31 32 00:02:30,470 --> 00:02:35,750 So we have this block of code which will be triggered whenever either of the answer buttons are pressed 32 33 00:02:36,170 --> 00:02:41,020 and we can change everything on the screen through these four outlets. 33 34 00:02:41,420 --> 00:02:49,100 Currently, when we run our app or that we see in our question label is just the words Question Text because 34 35 00:02:49,100 --> 00:02:53,870 that's what we added here as the starting sort of place holder values. 35 36 00:02:53,870 --> 00:02:58,430 But we want to replace that text with an actual question. 36 37 00:02:58,970 --> 00:03:06,710 And I want to pose this as a challenge to you. Without touching anything in the Main.storyboard, namely 37 38 00:03:06,890 --> 00:03:07,760 the Attribute 38 39 00:03:07,760 --> 00:03:15,920 Inspector, can you write some code so that even though our question label starts out in our Main.storyboard 39 40 00:03:15,920 --> 00:03:21,910 saying Question Text, as soon as the app runs, make it actually show a question? 40 41 00:03:21,920 --> 00:03:27,020 So in this case, it says, "Four + Two is equal to Six," and then the user would have to choose whether 41 42 00:03:27,020 --> 00:03:28,280 if that's True or False. 42 43 00:03:28,580 --> 00:03:33,980 So pause the video and try to complete that challenge and have a think about which part of your code gets 43 44 00:03:33,980 --> 00:03:40,600 triggered when the app is first loaded up. 44 45 00:03:40,800 --> 00:03:41,060 All right, 45 46 00:03:41,070 --> 00:03:42,690 so here's the solution. 46 47 00:03:42,780 --> 00:03:50,250 We have this function called viewDidLoad and already in the comments of our template, Apple already 47 48 00:03:50,250 --> 00:03:55,870 is hinting at us that this is the place where we do any additional setup after loading the view. 48 49 00:03:55,920 --> 00:04:02,010 This is the block of code that we can affect if we want something to happen as soon as the view loads 49 50 00:04:02,010 --> 00:04:04,410 up or as soon as the app appears on screen. 50 51 00:04:04,890 --> 00:04:12,510 So what we want to happen here is we want to tap into the questionLabel and change its text property 51 52 00:04:12,900 --> 00:04:20,790 to, say, "Four plus Two is equal to Six." 52 53 00:04:20,790 --> 00:04:27,390 Now, if we run our app, then you can see that even though our Main.storyboard starts out with Question 53 54 00:04:27,390 --> 00:04:32,290 Text, as soon as the view loads up, it says, "Four + Two is equal to Six." 54 55 00:04:32,370 --> 00:04:33,510 Brilliant. 55 56 00:04:33,510 --> 00:04:41,850 Now, a single question quiz app is not most people's idea of fun, so let's jazz it up with some more questions. 56 57 00:04:42,150 --> 00:04:45,480 And to do that, I'm going to create a array. 57 58 00:04:45,720 --> 00:04:52,410 So right up here just below my IBOutlets, I'm going to create a constant code quiz and it's going to 58 59 00:04:52,410 --> 00:04:55,560 be an array that stores three questions 59 60 00:04:58,170 --> 00:04:58,520 All right. 60 61 00:04:58,550 --> 00:05:08,700 So now I have a array of strings and I've got three items in here which are separated by commas. 61 62 00:05:08,780 --> 00:05:17,300 And now I can use this array to show my questions in my questionLabel, instead of having these bits 62 63 00:05:17,300 --> 00:05:19,010 of text floating around. 63 64 00:05:19,040 --> 00:05:23,510 So here's another challenge for you. Make the question label display 64 65 00:05:23,510 --> 00:05:27,660 the first question from our quiz array. 65 66 00:05:27,890 --> 00:05:30,280 So the end outcome should be the same. 66 67 00:05:30,280 --> 00:05:37,120 We should still see the same question on the screen, but we can no longer use it directly like so. 67 68 00:05:37,160 --> 00:05:42,450 Pause the video and review what you learn about arrays to see if you can complete this challenge. 68 69 00:05:46,030 --> 00:05:46,360 All right. 69 70 00:05:46,370 --> 00:05:53,510 So in order to complete this challenge, we have to use our quiz array. And in order to get the first question 70 71 00:05:53,540 --> 00:06:01,550 out of our quiz, we can simply add a set of square brackets and pick out the one at zero position which 71 72 00:06:01,550 --> 00:06:05,200 is, of course, the first question in our array. 72 73 00:06:05,270 --> 00:06:15,240 Now, when we run our app, we get exactly the same as before, but now we're using some more dynamic code, 73 74 00:06:15,570 --> 00:06:19,290 rather than just setting it manually. 74 75 00:06:19,440 --> 00:06:25,040 Now, even though we've got three questions, we can't actually progress through them yet. 75 76 00:06:25,170 --> 00:06:33,060 And ideally, we'd really like our answerButtonPressed function to be the trigger that changes the 76 77 00:06:33,060 --> 00:06:35,250 questionLabeled.text. 77 78 00:06:35,520 --> 00:06:42,750 So in order to do this, we first need a variable to keep track of which question the user is currently 78 79 00:06:42,750 --> 00:06:43,830 reading. 79 80 00:06:43,830 --> 00:06:52,070 Let's call it questionNumber and we'll start it out with zero because that would match with all zero 80 81 00:06:52,170 --> 00:06:54,990 position which is the first question. 81 82 00:06:54,990 --> 00:07:05,400 So now we can change this zero from using a hardcoded zero to using our questionNumber. So, again, if 82 83 00:07:05,400 --> 00:07:09,350 you run your app at this stage, it will work exactly the same as before, 83 84 00:07:09,540 --> 00:07:17,310 but we've now made our code even more dynamic and we can now increase our question number by one every 84 85 00:07:17,310 --> 00:07:23,610 time a answerButton gets pressed. So we can either write questionNumber equals the current value of 85 86 00:07:23,610 --> 00:07:25,950 question number plus 1 or, 86 87 00:07:25,950 --> 00:07:32,170 of course, we can use the shortened version that we've seen before which is just += 1. 87 88 00:07:32,210 --> 00:07:34,770 So here's a question for you to play computer. 88 89 00:07:34,860 --> 00:07:38,020 What do you think would happen if we ran the app at this stage? 89 90 00:07:38,070 --> 00:07:39,370 So don't run it, 90 91 00:07:39,420 --> 00:07:41,510 just think about what might happen. 91 92 00:07:41,550 --> 00:07:45,150 Do you think it will change the quiz text 92 93 00:07:45,150 --> 00:07:49,910 every time we press the answer button because we're increasing the questionNumber? 93 94 00:07:50,040 --> 00:07:52,760 Well, let's check it out. 94 95 00:07:53,090 --> 00:07:58,900 So when we press the True or False buttons, notice that nothing is changing about our 95 96 00:07:59,050 --> 00:07:59,480 questionLabel.text. 96 97 00:07:59,660 --> 00:08:00,890 So what's the reason here? 97 98 00:08:01,340 --> 00:08:06,860 Well, the only place where we're changing that questionLabel.text is, of course, inside of viewDidLoad 98 99 00:08:06,860 --> 00:08:12,950 block which only gets triggered once the first time the view loads up. 99 100 00:08:12,980 --> 00:08:18,950 So in order for this to keep changing, we actually need this line of code to be triggered again. 100 101 00:08:19,430 --> 00:08:25,670 So why don't we go ahead and create a function that captures this functionality and we'll call it update UI 101 102 00:08:25,670 --> 00:08:31,820 because in all likelihood, we're probably going to need this functionality a few times. 102 103 00:08:31,820 --> 00:08:39,050 So if I command X to cut it from there and paste it inside update UI, every single time I need that functionality 103 104 00:08:39,140 --> 00:08:41,680 I can now call my function Update UI. 104 105 00:08:42,140 --> 00:08:48,410 So I need the UI to update when the view loads up, but also after I've increased the question number. 105 106 00:08:49,010 --> 00:08:56,390 So now I've got my code in two places all triggering the same function which is to update the question 106 107 00:08:56,390 --> 00:09:00,260 label based on the current question number. 107 108 00:09:00,260 --> 00:09:02,330 So now if we run our app, 108 109 00:09:06,650 --> 00:09:14,030 you can see that when we press on this button, it will increase and go through our quiz questions. 109 110 00:09:14,030 --> 00:09:19,010 Now, of course, if you go beyond the third question, your app will crash. 110 111 00:09:19,010 --> 00:09:23,630 And this is, of course, because we only have three questions. If you're requesting for the fourth one, that 111 112 00:09:23,630 --> 00:09:24,540 doesn't exist, 112 113 00:09:24,830 --> 00:09:32,450 well, then we get a index out of range error. So our index which is currently number three is out of the 113 114 00:09:32,450 --> 00:09:36,630 range of our quiz because it only has 0, 1, and 2. 114 115 00:09:36,920 --> 00:09:40,460 And here's a quick way of seeing what's actually going on. 115 116 00:09:40,460 --> 00:09:46,850 Notice how when your app crashes, it highlights to you the line where the fatal error, which is really 116 117 00:09:46,850 --> 00:09:51,340 dramatic for Xcode to say, but this is where the fatal error occurred 117 118 00:09:51,350 --> 00:09:54,120 and that's what killed your app. 118 119 00:09:54,170 --> 00:10:01,460 Now, when your app crashes, you will see the debug console pop up, and you will see the useful messages, 119 120 00:10:01,580 --> 00:10:08,360 usually at the top of the debug console. So if you ever are in doubt, just scroll to the very top and 120 121 00:10:08,360 --> 00:10:09,980 read what it says. 121 122 00:10:09,980 --> 00:10:16,310 Now, the other thing you can do is you can expand this little dropdown list here, and you can see the 122 123 00:10:16,310 --> 00:10:20,210 value of some of the variables we have created. 123 124 00:10:20,210 --> 00:10:24,110 So currently at this point in time, when the app crashed, 124 125 00:10:24,110 --> 00:10:28,710 my questionNumber variable was equal to 3. 125 126 00:10:28,790 --> 00:10:38,690 Now, that means that I tried to pull out the quiz value at index 3, 0, 1, 2. Of course, 3 doesn't exist. 126 127 00:10:38,690 --> 00:10:44,170 So that's another way of helping you to debug and figure out what's wrong with your app. 127 128 00:10:44,270 --> 00:10:45,860 But there's no problems here. 128 129 00:10:45,860 --> 00:10:47,520 We expected that to happen. 129 130 00:10:47,570 --> 00:10:53,330 So let's go ahead and just stop our app. And in the next lesson, we're going to figure out how to not 130 131 00:10:53,330 --> 00:11:00,120 just solve this but also how to check the user's answer and see if they got the answer right. 131 132 00:11:00,200 --> 00:11:02,570 For all of that and more, I'll see you on the next lesson.