Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Let's say we want to put a variables value inside of a text field. An example of this would be if you're creating a game. And there's a text field that holds the score of the game. And every time the player scores a point, you want to update that text field with the player's current score. So, in order to do that, we're going to need to put a number variable inside of a text field. So, let's go to ViewController.h. And right below the line where I create the IBOutlet, going to go to the next line by creating a new line and type int score.
So again I'm declaring a score variable, and since this is not an IBOutlet, there is no circle to the left of my variable declaration. So save this file and go over to viewcontroller.m. Now right above label.text, I'm going to create a new line of code, and I'll type score. Again, we don't type int before it because we already declared it in the h file, and I'll set that equal to 10. So now you'll see, if I want to put the score in the text field, let's try deleting the string and typing the word score.
Now when I type that you will see an error message. That's that red sign with the exclamation point inside of it. When I click the error message, it says implicit conversion of int to NSString is disallowed. So what we need to do is convert that integer into a string. And here's how to do that. I'm going to change the word score to an open and close square braces. Open square brace, then a closed square brace. And put your cursor in the middle.
Then I'm going to type NSString. You might remember that NSString is the type of data that we use for a string value, or in other words, text. In addition to being a data type, NSString is also a class, which means it has commands that we can do. And, values inside of square brackets enable us to run commands. These kind of commands are called functions or methods, which we'll talk about later on. So, after NSString I'm going to type a space. And then I'm going to start to type string, and once I see stringWithFormat highlighted in the code hinting window, I'm going to press Return on my keyboard.
This command is very similar to the NSlog command, where we use a placeholder value to put a variable inside of a string. So let's look at how that works. Type an at symbol and two quotes. Inside the quotes, type score is and then the placeholder value. Remember, the placeholder is percent i. And again, that's a placeholder for some type of integer value. The area that we place the integer value is after the closed quote, we type a comma.
In the comma, we type the integer value. So here I could type 100 or whatever number I want and it'll show up in that string. I'm going to delete 100 and type score. Now we'll test the application in the simulator and we'll see score is 10. And there we go. I'm going to click the Stop button now, and now we can manipulate the score any way we want. For example, after the score is 10 line, I'm going to go to the next line and type score plus plus. When I test the application in the simulator, I see score is 11.
So the line of code where we set the value of the text field doesn't care what the value of score is. It's putting the value of the score variable, whatever it happens to be currently, inside the text field. So again, if you want to convert an integer value into a string so you can put it in a text field, use the NSString, stringWithFormat command. After the colon, type in NSString, which is an @ symbol and two quotes. Type in any text you want.
And then a placeholder value for your integer, which is percent i. After the close quote, type a comma, then the name of your variable, or an integer value. And then you'll get your integer value inside of a text field.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96674 Viewers
56 Video lessons · 110389 Viewers
71 Video lessons · 79213 Viewers
131 Video lessons · 37990 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.