0 1 00:00:00,270 --> 00:00:03,750 Once you've cloned the starting project, we need to do a better setup. 1 2 00:00:03,750 --> 00:00:08,730 This will give us a chance to understand the User Interface of the project stub and link the storyboard 2 3 00:00:08,790 --> 00:00:10,710 to the view controller. 3 4 00:00:10,710 --> 00:00:14,030 The first step is to take a look around the project, 4 5 00:00:14,040 --> 00:00:16,710 most importantly, the Main.storyboard. 5 6 00:00:17,280 --> 00:00:24,060 So here we've got a title label. And the first thing I would point out is if you want to have a large 6 7 00:00:24,120 --> 00:00:26,220 font size in your label, 7 8 00:00:26,220 --> 00:00:34,410 but if you want it to also be able to render on a smaller screen size. Say, for example, in this case, you 8 9 00:00:34,410 --> 00:00:43,130 can see that our text in our label has been truncated.,so our user can actually see the full message. 9 10 00:00:43,230 --> 00:00:45,490 Now, there's two ways of solving this. 10 11 00:00:45,510 --> 00:00:52,710 One is by changing the number of lines to zero which means that there's now no limit to the number of 11 12 00:00:52,710 --> 00:01:00,180 lines and it will try to keep the font size while creating as many lines as needed or that can fit into 12 13 00:01:00,180 --> 00:01:01,370 the space. 13 14 00:01:01,380 --> 00:01:08,370 So, for example, if I added a bit more text, you can see that it will also overflow because we run out 14 15 00:01:08,370 --> 00:01:11,250 of space in the vertical dimension. 15 16 00:01:11,250 --> 00:01:15,150 So in addition to setting the font size to zero, 16 17 00:01:15,240 --> 00:01:21,420 the other thing that you can do is to change the Autoshrink to have a minimum font size, so you can 17 18 00:01:21,420 --> 00:01:25,630 say that, well, we can shrink the font size, while normally it's 30, 18 19 00:01:25,650 --> 00:01:30,360 we can make it go down all the way to 15. So that if we had a lot of text, 19 20 00:01:33,360 --> 00:01:38,180 then it will simply just reduce the font size and keep everything visible. 20 21 00:01:38,250 --> 00:01:45,870 So those are two ways of manipulating labels so that the user will be able to see the text on all screen 21 22 00:01:45,870 --> 00:01:47,420 sizes. 22 23 00:01:47,460 --> 00:01:55,230 The next thing to notice is that there are three egg buttons: soft, medium, and hard, and there are three 23 24 00:01:55,380 --> 00:01:57,180 egg image views. 24 25 00:01:57,180 --> 00:02:01,650 Now, if you click on each of these buttons, you'll notice that they actually all have a title. 25 26 00:02:01,650 --> 00:02:02,630 This one says soft. 26 27 00:02:02,640 --> 00:02:06,300 This one says medium. But we can't actually see that, 27 28 00:02:06,300 --> 00:02:08,070 so what's going on? 28 29 00:02:08,070 --> 00:02:14,580 Well, remember how I said that in X code when you look at the document outline, you have to imagine that 29 30 00:02:14,580 --> 00:02:19,740 your eye sits right here, and you're looking at all the layers this direction. 30 31 00:02:20,400 --> 00:02:26,850 So that means that the egg image view is in fact on top of the egg button which is why we can't see 31 32 00:02:26,850 --> 00:02:31,170 the text of the button. Drag and change the order, 32 33 00:02:31,530 --> 00:02:36,540 then you will be able to see all of the text on the buttons. 33 34 00:02:36,570 --> 00:02:43,920 Now, why don't we simply just add an image or a background image to our button, so we could click this 34 35 00:02:43,920 --> 00:02:50,730 one, and then select hard egg? Well, it's because it's actually much more difficult to make the image view 35 36 00:02:50,790 --> 00:02:53,760 scale to fit on a button. 36 37 00:02:53,760 --> 00:03:00,570 So that's why we've actually left the image out and we've actually used a separate image view behind 37 38 00:03:00,570 --> 00:03:07,050 the button to show the user what's the hard egg looks like and what the soft egg looks like. 38 39 00:03:07,050 --> 00:03:13,470 Now, you can, of course, change the aspect fit using code, but this is just a much quicker and easier way 39 40 00:03:13,470 --> 00:03:14,580 of doing the same thing. 40 41 00:03:16,060 --> 00:03:23,770 So now all that's left to do is, of course, open up our assistant and we're going to link up our design 41 42 00:03:23,830 --> 00:03:26,070 with our code. 42 43 00:03:26,080 --> 00:03:27,840 Now, here's a challenge for you. 43 44 00:03:28,090 --> 00:03:36,250 Create a single IBAction called hardnessSelected and make sure that it's linked to all three buttons: 44 45 00:03:36,250 --> 00:03:44,920 Soft, Medium, and Hard, so that when they are touched, it will trigger a hardnessSelected IBAction. And 45 46 00:03:44,920 --> 00:03:51,130 then write some code, so that when you run the app and when you click on each of the buttons, it should 46 47 00:03:51,130 --> 00:03:59,290 print the Soft, Medium or Hard, namely the titles of each of these buttons depending on which one you 47 48 00:03:59,290 --> 00:04:05,210 pressed. Pause the video and give that challenge a go. 48 49 00:04:05,440 --> 00:04:07,390 So here's the solution. 49 50 00:04:07,390 --> 00:04:15,430 All we need to do is to create a single IBAction from each of these buttons, so make sure that you change 50 51 00:04:15,430 --> 00:04:21,820 from outlet to action and change the type of the connection to a UI button. 51 52 00:04:22,150 --> 00:04:29,540 And then we're going to change the name to hardnessSelectors, and then we're going to hit Connect. 52 53 00:04:30,070 --> 00:04:36,550 And now we can simply drag it from here and point out our Medium Egg Button as well as our Soft Egg button, 53 54 00:04:36,790 --> 00:04:41,700 so that they're all linked up. And notice because we've got quite a few things on the screen, 54 55 00:04:41,710 --> 00:04:47,890 it's actually more precise to use the document outline when you are dragging and dropping. 55 56 00:04:47,890 --> 00:04:55,240 So now that we've got our IBAction hardnessSelected, we can simply write a print statement which prints 56 57 00:04:55,300 --> 00:04:56,420 the sender. 57 58 00:04:56,440 --> 00:05:03,400 So remember, that was the button that triggered this IBAction, and then we're going to tap into one of 58 59 00:05:03,400 --> 00:05:10,100 its properties and it's gonna be the one that we used before which is the current title. 59 60 00:05:10,150 --> 00:05:17,800 So now when we click run, it will be able to print out the title of the button that we are selecting. 60 61 00:05:19,980 --> 00:05:27,570 So after doing a lot of research eating a lot of eggs, we've come to the realization that a soft egg 61 62 00:05:27,570 --> 00:05:33,690 should take about five minutes to boil, a medium egg about eight, and a hard egg about 12 minutes. 62 63 00:05:33,720 --> 00:05:39,190 Now, given that I can only really eat two eggs before I start feeling a bit woozy, 63 64 00:05:39,330 --> 00:05:44,150 this experiment took a long time. But we wanted to confirm what the Internet says, 64 65 00:05:44,160 --> 00:05:47,190 so these are the definitive numbers. 65 66 00:05:47,190 --> 00:05:51,410 So how do we add these numbers into our app? 66 67 00:05:51,420 --> 00:05:54,370 Well, we could create some constants, 67 68 00:05:54,390 --> 00:05:54,680 right? 68 69 00:05:54,810 --> 00:05:58,610 So right at the top of the file, we can create some constants 69 70 00:05:58,620 --> 00:06:01,980 and, remember, we create constants using the "let" keyword. 70 71 00:06:02,430 --> 00:06:08,880 And I'm going to create one called softTime which is going to be equal to five, and then mediumTime 71 72 00:06:08,940 --> 00:06:11,310 which is going to be equal to seven, 72 73 00:06:11,340 --> 00:06:18,660 And finally, hardTime, hardTime, and these indeed are hard times, 73 74 00:06:18,660 --> 00:06:20,590 so I'm going to change that to 12. 74 75 00:06:20,940 --> 00:06:28,770 And now we've got these three constants and we've also got this hardness which the user is selected, 75 76 00:06:28,830 --> 00:06:29,150 right? 76 77 00:06:29,250 --> 00:06:33,830 So we could, for example, add that to its own storage container. 77 78 00:06:33,840 --> 00:06:39,980 So we're going to create a new constant called hardness and we're going to set that equal to 78 79 00:06:39,990 --> 00:06:41,700 sender. currentTitle. 79 80 00:06:41,910 --> 00:06:46,990 And I'm actually going to delete that print statement because we don't need it anymore. 80 81 00:06:47,020 --> 00:06:54,420 Now, as soon as we do this, we get a warning from Xcode and it says, "Initialization of immutable 'hardness' 81 82 00:06:54,510 --> 00:06:59,550 was never used; consider replacing it with an assignment or removing it." 82 83 00:06:59,550 --> 00:07:06,990 Now, I want you to actually copy this entire warning message and paste it into Google and check out what 83 84 00:07:06,990 --> 00:07:10,560 StackOverflow has to say about what this means. 84 85 00:07:10,560 --> 00:07:17,600 And then once you're done, come back and we'll continue with the course. So let's paste in the important 85 86 00:07:17,600 --> 00:07:22,530 parts of that warning and let's see what Google gives us. 86 87 00:07:22,550 --> 00:07:27,080 So we've got a couple of different answers here. 87 88 00:07:27,200 --> 00:07:34,760 So the first one that we see seems to have a very specific question in terms of what they're doing wrong. 88 89 00:07:34,760 --> 00:07:39,440 So if we take a look at one of the other ones, you can see that this one is more generalized. 89 90 00:07:39,440 --> 00:07:42,800 It's trying to ask what this is actually all about. 90 91 00:07:43,250 --> 00:07:49,910 So the answer tells you that these changes are recommended because if you don't use these variables 91 92 00:07:49,910 --> 00:07:54,500 or constants, then you should consider replacing them with an underscore. 92 93 00:07:54,770 --> 00:08:02,960 The idea here is to say that, well, if you created a variable or a constant and you haven't used it yet, 93 94 00:08:03,020 --> 00:08:08,310 so consider replacing it with a underscore or consider removing it. 94 95 00:08:08,360 --> 00:08:11,840 But, of course, this is just Xcode being preemptive. 95 96 00:08:11,900 --> 00:08:17,180 And in fact at some point later on, we're definitely going to use our variables and constants. 96 97 00:08:17,180 --> 00:08:22,700 So just be aware that this is what this warning is about and that you can simply just copy and paste 97 98 00:08:22,760 --> 00:08:28,090 these warnings and errors into Google to be able to figure out what it means. 98 99 00:08:28,100 --> 00:08:28,400 All right. 99 100 00:08:28,430 --> 00:08:35,570 So now we have this constant code hardness and we have these different times that correspond to the 100 101 00:08:35,570 --> 00:08:37,100 different harnesses. 101 102 00:08:37,100 --> 00:08:39,730 So here's a challenge for you. 102 103 00:08:39,890 --> 00:08:47,780 How can we print the softTime, which is 5, if the hardness that was selected by the user was soft, 103 104 00:08:48,290 --> 00:08:53,460 and 7if they selected medium, and 12 if they selected hard? 104 105 00:08:53,480 --> 00:08:57,950 So if you have completed the challenge successfully, you should be at a press on this off button and 105 106 00:08:57,950 --> 00:09:04,070 it will print the soft time which is 5, medium that will print the medium time, and hard will print 106 107 00:09:04,070 --> 00:09:05,720 the hard time. 107 108 00:09:05,720 --> 00:09:10,040 So if you think you know how to solve this challenge, then go ahead and solve it. 108 109 00:09:10,100 --> 00:09:15,920 And that probably means that you know all about IF and ELSE and conditional statements in Swift, 109 110 00:09:15,920 --> 00:09:21,440 so you can go ahead and skip the next lesson. If you don't know how to solve this challenge, then continue 110 111 00:09:21,440 --> 00:09:26,540 on to the next lesson where we'll do a Swift Deep Dive on conditional statement, 111 112 00:09:26,540 --> 00:09:31,080 so IF and ELSE statement, and we'll learn how to be able to solve this challenge. 112 113 00:09:31,080 --> 00:09:33,180 So the choice is yours. Either way, 113 114 00:09:33,230 --> 00:09:34,820 I'll see you on the next lesson you choose.