Results 1 to 1 of 1
  1. #1

    Retired Prophet
    ToxicJew.'s Avatar
    Join Date
    Mar 2011
         User Info     Contact     Gamer ID
    Join Date
    Mar 2011

    Contact info:

    Youtube Channel:

    Default Xcode 4:Counting Machine for iPhone.

    In the last tutorial we made a simple switch, to toggle a label to say different things. In this tutorial, we're going to be making a simple counting application for an iPhone.

    For links to all of my tutorials, go to the bottom of the thread.

    This is similar to the basic addition calculator we made a few tutorials ago, but not the same. We're going to have an addition, subtraction, doubling, opposite, and reset button.

    So go ahead and open up Xcode, make a view based or single view application, for iOS, and name it TapCounter. This should all be very familiar and straightforward to you by now.

    Once you have your basic template created, go ahead and open the .h.

    In the .h we're going to be adding a variable, the number for the counter, an IBOutlet, and a few IBActions.

    Place this right between the #import and @interface lines:
    int count ;
    Next, dd a curly bracket on the same line as @interface, like so:

    And below that line, add an IBOutlet.
    	IBOutlet UILabel *number ;
    And close the first bracket, giving you this:

    Next we'll add the five IBActions.
    -(IBAction)add ;
    -(IBAction)subtract ;
    -(IBAction)reset ;
    -(IBAction)double ;
    -(IBAction)opposite ;
    So now you should have this:

    By now, i shouldn't have to explain any of this code, but if you don't understand anything, you need to use some of my other tutorials first. I provide detailed explanations of each line of code, but i've moved away from that as we move away from the most basic stuff.

    That's it for the .h, now we're moving to the .m.

    We'll start by defining what addition IBAction does.

    Add this right below the @implementation line:
    -(IBAction)add {
    	count++ ;
    	number.text = [NSString stringWithFormat:@"%i", count] ;
    This tells Xcode to track the variable we named in our very first coding step, int count, and to add one to it whenever the IBAction add is called upon. It also tells Xcode to generate a text number with the third line, "number.text……"

    Next you can copy that line you just added, because you're going to be repeating it, five times. You're going to be changing a few things in each one, but it's easier to just copy every one.

    Once you've done that, change your four new actions from add to subtract, reset, double, and opposite, respectively.

    So your .m would look like this:

    Next we're going to change the new actions to fit their names.

    Modify the subtract one so that it says:
    	count-- ;
    Modify the reset one so that it says:
    count = 0 ;
    Modify the double one so that it says:
    count = count*2 ;
    Modify the opposite one so that it says
    count = count - count*2 ;
    You can look at the above picture if you don't understand.

    That's it, we're done with coding. Now we're going to create the view.

    Browse to your .storyboard or .xib and open it up.

    Start by opening the Object Library, on the left column, and getting a label. Resize this label's font size to 82, center the text, and set the text to 0. This can be done in the Attributes Inspector, shown below.

    Next we'll add five buttons for our action. Select one button from the Object Library, set it's width and height to 58, and then highlight it, and press Command + D once to clone that button. Name the first one +, and the second one -.

    Next create three more buttons, set their width to 76, their height to 58, and name them Double, Oppos. and Reset, respectively. Arrange them however you like, i chose like this:

    Next right click the ViewController, the box on the right under the view in the above image. Drag from number, the variable under Outlets, to our big 0.

    Down at the bottom of that image, you'll see Received Actions. It has our IBActions we created. Drag each one to it's respective button. add to the +, subtract to the -, etc.

    When it asks you what you want to be performed to activate the action, select Touch Down.

    And that's it, your app is now ready to launch. Click Run, and see if it functions correctly.

    See if you can create a slightly more advanced counting machine on your own (maybe add some multiplication or division?)

    Thanks for reading, and as always leave your questions below.

    Also, check out this thread for all of my Xcode tutorials.

    0 Not allowed! Not allowed!
    Last edited by ToxicJew.; 03-15-2012 at 04:55 PM.
    ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้ส็็็็็็็็็็็็็็็็็็็็ ็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้

Tags for this Thread



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -10. The time now is 09:33 PM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.