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