Book Image

Mac Application Development by Example: Beginner's Guide

By : Robert Wiebe
Book Image

Mac Application Development by Example: Beginner's Guide

By: Robert Wiebe

Overview of this book

It's never been more important to have the ability to develop an App for Mac OS X. Whether it's a System Preference, a business app that accesses information in the Cloud, or an application that uses multi-touch or uses a camera, you will have a solid foundation in app development to get the job done.Mac Application Development by Example takes you through all the aspects of using the Xcode development tool to produce complete working apps that cover a broad range of topics. This comprehensive book on developing applications covers everything a beginner needs to know and demonstrates the concepts using examples that take advantage of some of the most interesting hardware and software features available.You will discover the fundamental aspects of OS X development while investigating innovative platform features to create a final product which take advantage of the unique aspects of OS X.Learn how to use Xcode tools to create and share Mac OS X apps. Explore numerous OS X features including iCloud, multi-touch trackpad, and the iSight camera.This book provides you with an illustrated and annotated guide to bring your idea to life using fundamental concepts that work on Mac.
Table of Contents (17 chapters)
Mac Application Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – creating the SimpleCalc interface


  1. Open the SimpleCalc project in Xcode.

  2. Click on the item named BTSAppDelegate.h and add the highlighted new interface @property definitions and the button action interface definition for the SimpleCalc App. Notice they are placed in the template code after the window property and before the @end statement as shown in the following code snippet:

    @interface BTSAppDelegate : NSObject <NSApplicationDelegate>
    
    @property (assign) IBOutlet NSWindow *window;
    
    /*
        Create the user interface elements
     */
    
    // Buttons
    @property (assign) IBOutlet NSButton *mAdd;
    @property (assign) IBOutlet NSButton *mSubtract;
    @property (assign) IBOutlet NSButton *mMutliply;
    @property (assign) IBOutlet NSButton *mDivide;
    
    // Text Fields
    @property (assign) IBOutlet NSTextField *mValue1;
    @property (assign) IBOutlet NSTextField *mValue2;
    @property (assign) IBOutlet NSTextField *mResult;
    /*
     Create the App interface for the buttons
     */
    
    - (IBAction)myButtonAction:(id)sender;
    
    @end

    Click on the item named BTSAppDelegate.m and add the highlighted new interface @synthesize definitions and the button action implementation for the SimpleCalc App.

  3. Again notice the position of the code after the @implementation keyword and before the @end keyword as shown in the following code snippet:

    @implementation BTSAppDelegate
    
    /*
     Create the user interface setter and getter functions
     */
    
    // Buttons
    @synthesize mAdd;
    @synthesize mSubtract;
    @synthesize mMutliply;
    @synthesize mDivide;
    
    // Text Fields
    @synthesize mValue1;
    @synthesize mValue2;
    @synthesize mResult;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    }
    
    /*
     Create the App implementation for the buttons
     */
    
    - (IBAction)myButtonAction:(id)sender;
    {
        // For now, just beep
        NSBeep();
    }
    
    @end
  4. Click on the item named MainMenu.xib.

  5. Click the Show Document Outline button as shown in the following screenshot:

  6. Click on the Window - SimpleCalc tab in Objects to display the App window as shown in the following screenshot:

  7. Locate the Object Library option and scroll to find the Square Button shape as shown in the following screenshot:

  8. Click and drag a Square Button shape from the Object Library to the SimpleCalc window. Then drag three more buttons and place them beside the first button to form a row of buttons. Finally, resize the window so that it fits the four buttons as shown in the following screenshot:

  9. Double-click on the buttons one at a time and type the labels +, -, ÷, x respectively.

    Tip

    To enter the ÷ character, hold down the option key while pressing / on the keyboard.

  10. You will end up with something that looks like the following screenshot:

  11. Locate the Object Library and scroll to find the Text Field option.

  12. Drag two Text Field objects into the SimpleCalc window, and place them one below the other.

  13. Locate the Object Library and scroll to find the Horizontal Line option.

  14. Drag a Horizontal Line into the SimpleCalc window and position it below the two Text Field objects.

  15. Locate the Text Field object in the Object Library and drag a third Text Field into the SimpleCalc window.

  16. Click and drag to select all the Square Button objects and drag them up below the third Text Field.

  17. Resize the SimpleCalc window to fit the Text Field and Square Button objects. After you are done, you will have something like the following screenshot:

  18. Because we don't want our calculator window to change size, we need to set constraints. Click on the SimpleCalc window to select it, select Show the size inspector in the inspector pane, and under Constraints, enable both the Minimum Size and Maximum Size options as shown in the following screenshot:

  19. Right-click (or if you don't have a three-button mouse, hold down the Ctrl key and left click) on the + button and drag it to the App Delegate object as shown in the following screenshot:

  20. When the App Delegate object highlights, release the mouse button and select myButtonAction from the Received Actions selection pane as shown in the following screenshot:

  21. Repeat the process to connect the remaining three buttons to the – myButtonAction method.

  22. Right-click (or if you don't have a three-button mouse, hold down the Ctrl key and left click) on the App Delegate object and drag until the first Text Field is highlighted as shown in the following screenshot:

  23. Release the mouse button and select mValue1 from the Outlets selection pane as shown in the following screenshot:

  24. Repeat these steps to connect mValue2 to the second Text Field and mResult to the third Text Field.

What just happened?

We created the interface for our SimpleCalc App and connected the interface to the program code for our App. We also connected the App Delegate object to the Text Fields in the interface. Now when we run SimpleCalc we see the new interface and when we click any of the buttons we hear a system beep.