Results 1 to 3 of 3
  1. #1

    Retired Prophet
    ToxicJew.'s Avatar
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,782
    Points
    7,587
         User Info     Contact     Gamer ID
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,782
    Points
    7,587

    Contact info:

    Youtube Channel: http://www.youtube.com/kennethbgoodin

    Default Xcode 4: Basic Addition Calculator.

    My last tutorials have focused on vocabulary, basic actions, and an overall overview of Xcode itself (links for them can be found at the bottom and in my sig). They've also focused on coding centered around iOS. Today, we will be moving away from that and creating a Cocoa application, a calculator for Mac OS X.

    Start by opening Xcode and selecting New Project. Select Application under Mac OS X, and then the first option, Cocoa Application.



    These screen should be familiar to you by now, but just in case, next you want to select the name. You can call it whatever you want, i'm using Simple Calculator.



    Choose where you want to save it, and then we can jump right in. Start by going to the .xib in the left column. The .xib is the same as a .storyboard file, meaning that this is where you will be crafting how your app looks.

    RIght now your .xib is blank, but don't worry, we'll be walking through adding everything you need. Start by going to the right column and bringing up the Object Library. If you don't know how to do this, it's explained in my HelloYou tutorial.

    In the Object Library find Window, and drag it onto the gridded view.



    Next you want to add a button, three text fields, and two labels. Change one of the labels to a "+" and one to a "=" and the button to "Calculate" and arrange them like so,



    Next right click on the first text field, the one to the left of the addition sign, and drag to the second one, the one to the right of the addition sign. Release the mouse button and select nextKeyView.



    Next, do the same thing, but right click the text field to the right of the addition sign, and drag to the left. Once again, select nextKeyView.

    This simply allows you to press Tab to switch between the two text fields.

    Next select the third text field, the one to the right of the equals sign. Browse over to the Attributes Inspector, in the right column. It's the fourth one over from the top. Once in the Attributes inspector, with the text field still selected, look for Behavior. Change the Behavior from Editable to Selectable.



    Then select the button that says Calculate and look at it's tab in the Attributes Inspector. Find the Key Equivalent slot; it should be blank. Click on it, and press Enter or Return. It should go from this:



    To this:



    This just tells Xcode that pressing the Return key on your keyboard simulates pressing that button.

    Next we're going to go to the Object Library again, search for Object, and drag the Object, the blue cube, over to the left sidebar, beside your window.

    In case you don't understand, drag this:



    Here:



    Then go back to the right column. Open the Identity Inspector, the third tab, and change the first field, the Class field. You can name it whatever you like, but for the sake of consistency mine will be called SimpleController.

    Change this:



    To This:



    Next we're going to select the first file in the left column, the highlighted one:



    And go up to File > New File, and select Objective-C Class.

    Make sure your first screen matches mine, it should look like this:



    For the next screen just uncheck the box near the bottom, and click Create.

    Now you'll see two files just below the top of the your left column: SimpleController.h and SimpleController.m, sound familiar? If you've been following my tutorials, this should seem like familiar stuff. These are essentially the same as the .h and .m from iOS coding.

    Let's jump right in to the coding.

    Select the .h, add a bracket after the @interface, and then add this right below that line:
    Code:
    @private
    	IBOutlet NSTextField *numOne ;
    	IBOutlet NSTextField *numTwo ;
    	IBOutlet NSTextField *answer ;
    }
    Your .h should now look like this:



    These three lines tell Xcode that we are going to have three text fields: numOne, numTwo, and answer.

    Next we're going to add an IBAction. If you remember from the previous tutorials, an IBAction is any action you want it to be, depending on how you define it in the .m. In the .h, you simply put the name of the action.

    Start by adding this:
    Code:
    -(IBAction)Calculate:(id)sender ;
    This just tells Xcode that we are going to have an action called Calculate.

    Now your .h should look like this:



    Next we'll be looking at the .m file.

    Before we start, if your .m has anything between @implementation and @end, delete it so that it looks like this:



    Start by adding an IBAction right below the @implentation:
    Code:
    -(IBAction)Calculate:(id)sender {
    	int result = [numOne intValue] + [numTwo intValue] ;
    [answer setIntValue: result] ;
    }
    This may seem like a lot, but it's actually pretty simple.

    We start by declaring what our IBAction is, Calculate, which we mentioned in the .h. Then we say what it does. We set the value of result equal to the sum of numOne and numTwo, and then we set the variable answer equal to whatever result is.

    Your .m should now look like this:



    Next go up to the menu, select Product > Build and your build should succeed.



    Now open your .xib again.

    Remember that Object we created earlier, the blue box on the bottom of the left column? Right click and drag that to the first, second, and third text fields, and select their respective parameter. Select numOne for the first box, numTwo for the second, and answer for the third.



    Next right click the Calculate button, and drag it to the SimpleController, the blue box we created. If it won't let you select the SimpleController when you're dragging it, check your code, you did something wrong.

    Once you drag it, a box will pop up with the option, Calculate. Select it.



    Now go to Product > Build, and then you're done. You can now test your calculator yourself, and attempt writing a more complex one.

    Here's a final image of mine working. Thanks for reading, and if you have any questions, problems, concerns, post them below.



    My other Xcode tutorials:

    0 Not allowed! Not allowed!
    Last edited by ToxicJew.; 03-14-2012 at 11:33 AM.
    ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้ส็็็็็็็็็็็็็็็็็็็็ ็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้

  2. #2
    mini mini's Avatar
    Join Date
    Aug 2011
    Location
    Minnesota
    Posts
    1,988
    Points
    2,840
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Location
    Minnesota
    Posts
    1,988
    Points
    2,840
    Gamer IDs

    PSN ID: minnesotastyle

    Contact info:

    Youtube Channel: www.youtube.com/user/codeleakerforums

    Default

    Good tutorial...could you post what site youre getting all this info from or is this all you?

    0 Not allowed! Not allowed!


  3. #3

    Retired Prophet
    ToxicJew.'s Avatar
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,782
    Points
    7,587
         User Info     Contact     Gamer ID
    Join Date
    Mar 2011
    Location
    S̢͎̳̞̲͈̪̳̻ͮͩt̟̳̏ͬ̔&#
    Posts
    7,782
    Points
    7,587

    Contact info:

    Youtube Channel: http://www.youtube.com/kennethbgoodin

    Default

    This is all me, pictures, words, everything. I learned Objective-C through some books i read back when i started, and working hands on with the language. If you want to learn, you can check out the Developer section of Apple's site, or read some guides online. I've got one posted on here as well, somewhere.

    Thanks for the appreciation.

    0 Not allowed! Not allowed!
    ส็็็็็็็็็็็็็็็็็็็็็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้ส็็็็็็็็็็็็็็็็็็็็ ็็็็็༼ ຈل͜ຈ༽ส้้้้้้้้้้้้้้้้้้้้้้้

Tags for this Thread

Bookmarks

Bookmarks

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 02:41 AM.
Powered by vBulletin®
Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.