Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Solution: Create two variables

From: Programming for Non-Programmers: iOS 7

Video: Solution: Create two variables

There are multiple ways you could have approached this challenge. And as long as you arrive at the Here is the method that I used. So, there is one. And there is another label.

Solution: Create two variables

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.

Show transcript

This video is part of

Image for Programming for Non-Programmers: iOS 7
Programming for Non-Programmers: iOS 7

42 video lessons · 8069 viewers

Todd Perkins
Author

 
Expand all | Collapse all
  1. 5m 18s
    1. Welcome
      44s
    2. What you should know before starting this course
      1m 17s
    3. Related courses
      1m 8s
    4. Using the exercise files
      1m 19s
    5. Viewing the finished app
      50s
  2. 10m 24s
    1. Finding and installing Xcode
      35s
    2. Creating an Xcode project
      1m 55s
    3. Understanding the Xcode interface
      3m 54s
    4. Configuring Xcode for app development
      2m 14s
    5. Configuring the iOS Simulator for app development
      1m 46s
  3. 43m 43s
    1. Understanding how programming works
      2m 34s
    2. Understanding variables
      2m 56s
    3. Using primitive variables
      9m 10s
    4. Using pointer variables
      4m 51s
    5. Using instance variables
      5m 19s
    6. Connecting visual objects to variables
      8m 12s
    7. Placing a number variable in a string
      4m 33s
    8. Challenge: Create two variables
      54s
    9. Solution: Create two variables
      5m 14s
  4. 27m 14s
    1. Understanding functions, methods, and selectors
      4m 43s
    2. Using functions, methods, and selectors
      7m 1s
    3. Understanding parameter functions
      2m 10s
    4. Connecting a button to a function
      7m 47s
    5. Challenge: Create a counter app
      43s
    6. Solution: Create a counter app
      4m 50s
  5. 13m 38s
    1. Understanding conditional statements
      2m 35s
    2. Using conditional statements
      5m 21s
    3. Challenge: Build an on/off button app
      39s
    4. Solution: Build an on/off button app
      5m 3s
  6. 44m 59s
    1. Viewing the app's code structure
      2m 11s
    2. Setting up the user interface
      7m 9s
    3. Setting up variables and functions
      5m 8s
    4. Connecting all of the visual elements to code
      2m 59s
    5. Displaying tapped numbers in the calculator
      4m 47s
    6. Controlling when tapped numbers should not appear in the calculator
      3m 27s
    7. Making the Clear button clear all values
      1m 4s
    8. Setting the calculator to add or subtract
      4m 29s
    9. Showing the total when the equals button is tapped
      3m 40s
    10. Formatting a number with commas
      5m 25s
    11. Challenge: Add a multiplication button
      1m 5s
    12. Solution: Add a multiplication button
      3m 35s
  7. 24s
    1. Next steps
      24s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Programming for Non-Programmers: iOS 7.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.