Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are multiple ways you could have approached this challenge. And as long as you arrive at the correct solution, any method you use can be correct. Here is the method that I used. In mainstoryboard.storyboard, I made sure that my utility view was showing, and then I clicked and dragged two labels onto the screen. So, there is one. And there is another label. Then, I created two variables, one for each label.
I did that in ViewController.h. At the end of line 11, I will go to the next line, create an open curly brace and type enter. And now I'm going to create an IBOutlet that is a UI label called name. At this point, we need two different UI labels. I'm going to show you a short cut to doing that. One way is, we can select this line and copy with Cmd+C. Go to the next line, paste the code with Cmd+V, and change name to age.
But since there are two variables that are the same data type, that is IBOutlet, UILabel. We actually have a short cut. I'll show you how to do that. Going to delete that line of code. And after name and before the semi colon. I'm going to type a comma, asterisk and then age. This is the same as the two line code method I used. It's just a short hand way for writing the same thing. So, here I've created two variables. One called name. And one called age.
Of course, I didn't expect you to know that going in, I just wanted to show you a shorthand technique for creating multiple variables of the same type. Back to mainstoryboard.storyboard. Now I need to connect my labels to my code. Remember, you click on View Controller on the left side, and that's under View Controller Scene, so you might have to expand that, and then click on the code connections button, which is all the way on the right. It's a circle with an arrow inside of it. And then, under Outlets, you should see age and name.
So, I'm going to click and drag from this circle for age onto the bottom label. And then for name, I'm going to click and drag from the circle to the top label. With those code connections made, I'm going to save the file. Now, I'll go over to viewController.m. And as usual, I'm going to hide the utility view because I no longer need it. In viewdidload, right below the comment, I'm going to type name.text, and I'm going to set that equal to an NSString, which is an at symbol with quotes.
Don't forget the semicolon at the end of the line. Now, I'll type my name inside of the string. So, I put Todd in there, and then I'll go to the next line. So that one was really easy. The hard part is putting a number inside the text field. But there is a cheating way to do that. If I type age.text, I can just set it equal to the NSString in quotes. Zero, of course, because my age is a secret. So, instead of using the variable, you could have just typed the number of your age inside of the string. And that is a fine solution.
There's nothing wrong with using that method. So, when I test this in the simulator, you'll see Todd and zero. But let's say you wanted to use a number variable. Right below name.text, I'm going to create an integer called myAge, and I'm using camel case, so the A is capitalized, equals, and let's say 100 here. Again, semicolon to end the statement. And I want to put that age variable inside of the text field. Remember, you can do that by using the command NSString, string with format. So, I'm going to delete the @ in the quotes and the zero on the line 23 in my code, I'm going to type an open and close square bracket.
And inside of the square brackets, NSString. Now, I'm using the code hinting by pressing Return, string with format. Again, I'm pressing Return. Having Xcode write that for me. This makes sure I don't type it incorrectly and saves time. So, remember, here, we're going to type this blank string first. That is an at symbol with quotes. And now, we're going to put, inside of the quotes, the placeholder for an integer variable which is %i. After the close quote, type a comma, and then the name of your variable, which in this case, is my age.
Now, when we test the app in the simulator, we'll see Todd, and 100. Again, however you approach this challenge, as long as you get your name and age in the text fields, and you used code to get it in there, it's correct. But, hopefully, you learned something new in this process.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 100984 Viewers
61 Video lessons · 87767 Viewers
71 Video lessons · 71655 Viewers
56 Video lessons · 103544 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.