Chapter 1 to 5


Published on

Covers CH 1-5 of Beginning Mac Programming, creating TextApp

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Start Xcode, and let’s create our first project. Upon launch, you should see the welcome splash page, which resembles the page shown here. Let’s create a new Xcode project.

    If you have opted to not show this window when Xcode launches, then you can start a new project by clicking File >> New Project in the menu above.
  • Xcode provides several template projects you can use as starting points. Select “Application” under “Mac OS X” on the lower left panel, and select “Cocoa Application” from the upper right panel. For now, leave all the options on the lower left panel unchecked, and click the “Choose...” button in the lower right corner of the window.
  • You’ll see a standard Mac OS X save window. We’re going to call this project “TextApp”.

    It’s a good idea for you to create a master folder to save the project files from our sessions. You can see a more expanded view by clicking the down arrow button on the right of the “Save As” field. From there you can select or create a folder to save your projects into. I have created a folder in my personal workspace called “Beginning Mac Programming”, and I’m saving all my projects from this book in that folder.
  • Now you can see the Xcode project window with the template files for your project.

    Look at the Groups and Files list on the left. Most of the time we will spend our time working with the files listed under the first group, the “TextApp” group in this case. For now, though, click on the “Other Sources” folder to expand it and view its contents.

    You will see two files, one of which is called main.m. Click this file to see the contents in the lower-main portion of the project window.

  • Assuming you haven’t modified Xcode’s default settings, the code shown here will be in a variety of colors. This is part of the Xcode IDE (integrated development environment), and is designed to help you identify various components of your code “at a glance”.

    * Comments are shown in green.
    * Precompiler directives are shown in brown. File names that are part of the precompiler directives are shown in orange.
    * Keywords are shown in fuschia.
    * Cocoa class names, message calls and method names are shown in purple.
    * Other colors are used to identify other key elements. Elements that don’t fall into a specific category are shown in black text.

    Every C program, including an Objective-C application, contains a “main” function, shown here. Most of the time, you won’t be making any changes to the “main” function that is created from the project templates. When you get into more advanced projects, you may find it necessary to modify this function.

    Now, look a the single line that falls between the curly brackets. This line launches and hands control over to a Cocoa application. The main function is executed when you run your application, which in turn launches the Cocoa application you create.

    Now we’re going to look at the Interface Builder, which is used to create the user interfaces of your application and generate the code that connects your programmed classes to the user interface. In the “Groups and Files” pane on the left, click on the “Resources” folder and double-click on the MainMenu nib file, called “MainMenu.xib.
  • Nib files are named for the acronym “NextStep Interface Builder” files, although now you typically see those files named with an “.xib” extension. The actual source code of a NIB file is in XML format; hence the “x” in the “.xib” extension.

    You’ll see a number of windows on your screen.
  • The main MainMenu.xib window, shown here, contains a number of objects. Double click the “Main Menu” icon to open the menu editor.
  • The menu editor contains the menu bar that is displayed at the top of the screen when you run your application. Clicking on a menu item displays the drop-down menu for that item.

    Click the “TextApp” menu to display it, and double-click the first menu item. You can then edit the contents.

    Change it to “About My Wonderful TextApp Application”.

    When you’re finished, save the MainMenu.xib file to save your menu changes.

    From the Tools menu, open the Library Pallette.
  • You’ll see a number of built-in controls that are part of the Cocoa framework in this palette.

    At the bottom of the Library palette window is a search box; type “text view” into this box to narrow your selection.
  • Let’s add a text view to the TextApp window. If it’s not visible, double-click the “Window (TextApp)” icon in the MainMenu nib file window.

    Click the “Text View” object, and drag it over and drop it on the blank TextApp window. Expand the text view object by dragging the corners out until you see the blue guidelines show up at the edges of the TextApp window.
  • You should have something that looks similar to this.

    Save your Interface Builder file, and switch back to Xcode.

    That finishes through Chapter 2. If you want to see the state of your application, click “Build and Run” in Xcode and see your application. When you’re finished, click the red “Tasks” button to terminate the application.

    Now we’re going to write some code to go with our application.
  • In Xcode’s “Groups and Files” pane on the left, right click on the “Classes” folder, and click “Add >> New File”.
  • The New File window should appear.

    On the lower left (under Mac OS X), click “Cocoa Class”, and select the “Objective-C class” template on the upper right pane. Leave the default “NSObject” subclass selection in the drop-down box in the middle pane, and click “Next”.

    Change the name to “NotifyingClass.m”, make sure the “Also create NotifyingClass.h” checkbox is selected and click “Finish”.
  • You should see two files listed under the “Classes” group in your project window, and the source code panel should display the “NotifyingClass.h” contents.

    We’re going to create a simple class that notifies us when it is created in the Console. NotifyingClass inherits a great deal of functionality from it’s parent class, NSObject, but we’ll be overriding the parent class called “init” to create our notifying message later.

    For now, save this file, and double click the MainMenu.xib file name under the “Resources” folder in the left hand pane to open Interface Builder.
  • Pull up the Library Palette (“Tools >> Library”) and type “object” in the search box.

    Click on the “Object” selection and drag and drop it into the MainMenu.xib window.

    Select the new “Object” icon and click the “Inspector” icon at the top of the MainMenu.xib window.
  • Click the “Identity Inspector” icon on the far right of the “Object Identity” window, and change the name of the Class to “NotifyingClass” in the drop-down box at the top of the window. If you saved your NotifyingClass source files in Xcode before you launched Interface Builder, you should see the name “NotifyingClass” prefill in this field.

    Save your MainMenu.xib file in Interface Builder and return to Xcode.
  • Now we’re going to write the method that gets executed when our application gets launched.

    Open the NotifyingClass.m file.

    Click “File >> Open Quickly...” from the Xcode menu, and type “NSObject”, and select the NSObject header file (.h), then click “Open”.

    Scroll down and find the relevant lines that define the NSObject interface. Copy the line “- (id)init;” to your clipboard.

  • Switch back to the “NotifyingClass” implementation file and paste the line between the @implementation and @end lines.

    Remove the semicolon and replace it with a pair of curly brackets.
  • Add a line that logs a message to the Console.

    Type in “NSLog(@”Hello World! I’m a new NotifyingClass instance!”);

    followed by

    return self;

    This will log the message “Hello World! I’m a new NotifyingClass instance!” to the console when you launch your application, and then return the initialized Notifying Class object to the application, as identified in the signature of the method by the “id” in parentheses.

    Save your file, click “Build and Run”, click the red “Tasks” icon and switch to the console by clicking Command > Shift > R. You’ll see the message logged in the Console when the NotifyingClass object is initialized.

    Now, let’s switch back to the Interface Builder and add a second NotifyingClass object to the application. Drag another base Object from the Library palette and drop it into the MainMenu.xib window. Change the name of the object to “NotifyingClass” in the Identity Inspector and save your changes in Interface Builder.

    Return to Xcode, run your application again, click the “Stop Tasks” icon and pull up the console.
  • You should see two messages logged into the console, one from each instance of the NotifyingClass object.

    This carries us through the end of Chapter 3.
  • Now we’re going to change our NotifyingClass method to actually send a message to the TextApp text window.

    Click on the NotifyingClass header file to display the code, and add a signature as follows:

    - (void)displaySomeText;

    to identify a new method.

    The “void” keyword in parentheses indicates that this method will not be returning a value to the application. Since the method name does not contain any arguments (designated by one or more colons followed by a parameter designation in the method name), you will not be passing any information to the method when you send a message to the NotifyingClass object.

    Switch to the NotifyingClass implementation file, and add the method. In the method insert a NSLog statement that logs the message “displaySomeText just got called!” to the console.

    Save your implementation file and switch back to the header file.
  • Now, we need to add a parameter to our new method. We will be sending a message object to this method from an object in the user interface. We can designate this by adding the parameter “(id)sender” to our method name, which specifies that some kind of object will be provided as the message sender.

    We also want to indicate to Interface Builder that this method is available to receive an action from a user interface object. We do this by changing the keyword “void” to “IBAction”.

    Save your header file and switch to the NotifyingClass implementation file.
  • Make the same changes to the method signature, and save the implementation file.

    Double click the MainMenu.xib file name in the left hand pane to pull up Interface Builder.

  • Pull up the Library Palette and type “button” in the search box. Find a “push button” object an drag and drop it into the TextApp Window. You may have to move the bottom of the existing text box object up to make room.
  • In the MainMenu nib file window, delete one of the NotifyingClass object instances, then right-click on the remaining NotifyingClass object You should see a Heads-Up Display (HUD) with the name of your newly created method that can receive actions under the “Received Actions” header -- displaySomeText.

    Click and drag from the circle to the right of the method name to the newly added push button object in your TextApp window, and release your mouse.
  • Now right click on the button in the TextApp window, and you should see another HUD that shows the “displaySomeText” method name under the “Sent Actions” heading, and the object name “Notifying Class” to the right of it. this indicates that you have now created a connection between the action sent by the push button and the newly defined method in your NotifyingClass code.

    Save your file in Interface Builder, switch back to Xcode and click “Build and Run”. Pull up the console to see the newly logged message...
  • Now, instead of sending messages to the console, we want our users to see a message in the text box whenever we click the button. In order for this to happen, our NotifyingClass object needs to be able to converse with the NSTextView object in the TextApp window. To do this, we need to add a “pointer” to the text view.

    Open the NotifyingClass header file in Xcode.
  • The pointer to the text view will be an attribute for the NotifyingClass, so we need to modify the class description to add this attribute.

    Add “NSTextView *textView;” between the curly braces as shown.

    Since we are going to be pointing this attribute to an object in user interface using Interface Builder, we need to indicate that this is available to Interface Builder using another keyword, IBOutlet. This indicates to Interface Builder that this pointer is available as an “outlet” to have an interface object “plugged in” to it.

    Save the header file and switch to Interface Builder by clicking on the MainMenu nib file name in the left hand panel.
  • Right click the “Notifying Class object in the MainMenu nib file window to open the HUD. You should see an “Outlets” group that contains one item - the name of our textView attribute that we created as an outlet in Xcode.

    Link this outlet to the text view object in the TextApp window.

    Save your work in Interface Builder and switch back to Xcode.

    To send a message to the text view object, we need to know a little about that object. Open the Developer Documentation and pull up the NSTextView Class Reference.
  • In the column immediately to the left of the documentation, expand the “Tasks” heading and look at the message categories. Find the one called “Inserting Text” – that looks like it’s just what we’re looking for – and click it.

    You’ll see that the first message listed is the “insertText:” message – again that looks like what we need. Click that.
  • You should see the window jump down to the documentation for the “insertText:” message.

    insertText: takes one attribute, a string object. Excellent!

    Now that we know what message to send to interact with the text view object, close the documentation and open the NotifyingClass implementation file.
  • In Objective-C, the syntax for sending a message to an object is “open square bracket”, object pointer name, message (with parameters), “close square bracket”.

    Remove the NSLog statement from the displaySomeText method and replace it with the message we want to send to the text view:

    [textView insertText:@”displaySomeText just got called!

    The square brackets indicates to Objective-C that we are sending a message. Also note that we’ve added a backslash-n to the end of the string object we’re sending -- this will insert a newline character at the end of the string sent to the text view window.

    Save your changes in Xcode, and click “Build and Run”
  • When you click the button, you should see your message displayed in the text view window. Every time you click the button you should see a new message added.

    Now we’re going to send an additional message to our Button object, which will change the name of the button and deactivate it once you click it.

    First let’s look up the documentation for the NSButton Class Reference.
  • Since we are looking to change characteristics of the button in the TextApp window, expand the “Tasks selection in the middle left column. Click on the subheading “Configuring Buttons”, and select the “setTitle:” message on the right.
  • We see that the “setTitle:” message takes one parameter, a string object, and does not return anything to the message sender.

    Close the documentation and pull up the NotifyingClass implementation file.

  • Let’s add a new message to the displaySomeText: method, that sends a message back to the sender of the action (in this case our NSButton Object).

    [sender setTitle:@”Clicked!”];

    We also want to disable our button once we’ve clicked it.

    Open the documentation for the NSButton Class Reference again.
  • We don’t see any message categories in the “Tasks subheading that fit our needs. However, the NSButton class inherits behaviors from a number of super classes, including the NSControl class – let’s pull up the documentation for that class reference by clicking on the hyperlink in the NSButton Class Reference documentation for NSControl.
  • Expand the “Tasks” subheading to the left of the documentation panel, and peruse the action categories. Since we want to disable the button once we click it, let’s look at the actions grouped under “Enabling and Disabling the Control”. Click this.
  • This message subcategory has two messages; the one that appears to meet our needs is “setEnabled:”. Click that to see the documentation of that message.
  • OK. This is it.

    This message takes one parameter, a BOOL flag. Objective-C has keywords established for BOOL flags: YES and NO. Since we want to disable our button once it has been clicked, we need to pass a NO flag as a parameter to our message.

    Close the documentation and switch back to Xcode to the NotifyingClass implementation source code
  • Let’s send one more message to the button object:

    [sender setEnabled:NO];

    Save your changes and click Build and Run.
  • This time, you can only click the button once, and send one message to the text view object.

    This wraps up Chapter 4.

  • At this point we need to switch to the Objective-C documentation slides.
  • Chapter 1 to 5

    1. 1. The Project Window
    2. 2. (1) (2) (3)
    3. 3. Storing Information in Memory • Computer memory is similar to a giant collection of switches – each switch is either “on” or “off”. Each of these switches is called a “bit”. • Since each switch can have only two states, we refer to this as “binary notation”. • A series of “bits” is combined to make up a “byte”, which is generally comprised of 8 bits. • Luckily, we don’t have to work in “0s” and “1s” – Objective-C, like most higher level programming languages, takes care of the translation from “human readable” to “binary” for us.
    4. 4. Variables • We can use variables to store and retrieve information from memory. • Variables are basically names we can assign to blocks of memory that allow us as programmers to work with that memory more easily.