Start Xcode, and let&#x2019;s create our first project. Upon launch, you should see the welcome splash page, which resembles the page shown here. Let&#x2019;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 &#x201C;Application&#x201D; under &#x201C;Mac OS X&#x201D; on the lower left panel, and select &#x201C;Cocoa Application&#x201D; from the upper right panel. For now, leave all the options on the lower left panel unchecked, and click the &#x201C;Choose...&#x201D; button in the lower right corner of the window.
You&#x2019;ll see a standard Mac OS X save window. We&#x2019;re going to call this project &#x201C;TextApp&#x201D;.
It&#x2019;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 &#x201C;Save As&#x201D; 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 &#x201C;Beginning Mac Programming&#x201D;, and I&#x2019;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 &#x201C;TextApp&#x201D; group in this case. For now, though, click on the &#x201C;Other Sources&#x201D; 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&#x2019;t modified Xcode&#x2019;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 &#x201C;at a glance&#x201D;.
* 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&#x2019;t fall into a specific category are shown in black text.
Every C program, including an Objective-C application, contains a &#x201C;main&#x201D; function, shown here. Most of the time, you won&#x2019;t be making any changes to the &#x201C;main&#x201D; 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&#x2019;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 &#x201C;Groups and Files&#x201D; pane on the left, click on the &#x201C;Resources&#x201D; folder and double-click on the MainMenu nib file, called &#x201C;MainMenu.xib.
Nib files are named for the acronym &#x201C;NextStep Interface Builder&#x201D; files, although now you typically see those files named with an &#x201C;.xib&#x201D; extension. The actual source code of a NIB file is in XML format; hence the &#x201C;x&#x201D; in the &#x201C;.xib&#x201D; extension.
You&#x2019;ll see a number of windows on your screen.
The main MainMenu.xib window, shown here, contains a number of objects. Double click the &#x201C;Main Menu&#x201D; 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 &#x201C;TextApp&#x201D; menu to display it, and double-click the first menu item. You can then edit the contents.
Change it to &#x201C;About My Wonderful TextApp Application&#x201D;.
When you&#x2019;re finished, save the MainMenu.xib file to save your menu changes.
From the Tools menu, open the Library Pallette.
You&#x2019;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 &#x201C;text view&#x201D; into this box to narrow your selection.
Let&#x2019;s add a text view to the TextApp window. If it&#x2019;s not visible, double-click the &#x201C;Window (TextApp)&#x201D; icon in the MainMenu nib file window.
Click the &#x201C;Text View&#x201D; 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 &#x201C;Build and Run&#x201D; in Xcode and see your application. When you&#x2019;re finished, click the red &#x201C;Tasks&#x201D; button to terminate the application.
Now we&#x2019;re going to write some code to go with our application.
In Xcode&#x2019;s &#x201C;Groups and Files&#x201D; pane on the left, right click on the &#x201C;Classes&#x201D; folder, and click &#x201C;Add >> New File&#x201D;.
The New File window should appear.
On the lower left (under Mac OS X), click &#x201C;Cocoa Class&#x201D;, and select the &#x201C;Objective-C class&#x201D; template on the upper right pane. Leave the default &#x201C;NSObject&#x201D; subclass selection in the drop-down box in the middle pane, and click &#x201C;Next&#x201D;.
Change the name to &#x201C;NotifyingClass.m&#x201D;, make sure the &#x201C;Also create NotifyingClass.h&#x201D; checkbox is selected and click &#x201C;Finish&#x201D;.
You should see two files listed under the &#x201C;Classes&#x201D; group in your project window, and the source code panel should display the &#x201C;NotifyingClass.h&#x201D; contents.
We&#x2019;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&#x2019;s parent class, NSObject, but we&#x2019;ll be overriding the parent class called &#x201C;init&#x201D; to create our notifying message later.
For now, save this file, and double click the MainMenu.xib file name under the &#x201C;Resources&#x201D; folder in the left hand pane to open Interface Builder.
Pull up the Library Palette (&#x201C;Tools >> Library&#x201D;) and type &#x201C;object&#x201D; in the search box.
Click on the &#x201C;Object&#x201D; selection and drag and drop it into the MainMenu.xib window.
Select the new &#x201C;Object&#x201D; icon and click the &#x201C;Inspector&#x201D; icon at the top of the MainMenu.xib window.
Click the &#x201C;Identity Inspector&#x201D; icon on the far right of the &#x201C;Object Identity&#x201D; window, and change the name of the Class to &#x201C;NotifyingClass&#x201D; 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 &#x201C;NotifyingClass&#x201D; prefill in this field.
Save your MainMenu.xib file in Interface Builder and return to Xcode.
Now we&#x2019;re going to write the method that gets executed when our application gets launched.
Open the NotifyingClass.m file.
Click &#x201C;File >> Open Quickly...&#x201D; from the Xcode menu, and type &#x201C;NSObject&#x201D;, and select the NSObject header file (.h), then click &#x201C;Open&#x201D;.
Scroll down and find the relevant lines that define the NSObject interface. Copy the line &#x201C;- (id)init;&#x201D; to your clipboard.
Switch back to the &#x201C;NotifyingClass&#x201D; 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 &#x201C;NSLog(@&#x201D;Hello World! I&#x2019;m a new NotifyingClass instance!&#x201D;);
This will log the message &#x201C;Hello World! I&#x2019;m a new NotifyingClass instance!&#x201D; 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 &#x201C;id&#x201D; in parentheses.
Save your file, click &#x201C;Build and Run&#x201D;, click the red &#x201C;Tasks&#x201D; icon and switch to the console by clicking Command > Shift > R. You&#x2019;ll see the message logged in the Console when the NotifyingClass object is initialized.
Now, let&#x2019;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 &#x201C;NotifyingClass&#x201D; in the Identity Inspector and save your changes in Interface Builder.
Return to Xcode, run your application again, click the &#x201C;Stop Tasks&#x201D; 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&#x2019;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:
to identify a new method.
The &#x201C;void&#x201D; 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 &#x201C;displaySomeText just got called!&#x201D; 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 &#x201C;(id)sender&#x201D; 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 &#x201C;void&#x201D; to &#x201C;IBAction&#x201D;.
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 &#x201C;button&#x201D; in the search box. Find a &#x201C;push button&#x201D; 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 &#x201C;Received Actions&#x201D; 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 &#x201C;displaySomeText&#x201D; method name under the &#x201C;Sent Actions&#x201D; heading, and the object name &#x201C;Notifying Class&#x201D; 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 &#x201C;Build and Run&#x201D;. 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 &#x201C;pointer&#x201D; 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 &#x201C;NSTextView *textView;&#x201D; 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 &#x201C;outlet&#x201D; to have an interface object &#x201C;plugged in&#x201D; 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 &#x201C;Notifying Class object in the MainMenu nib file window to open the HUD. You should see an &#x201C;Outlets&#x201D; 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 &#x201C;Tasks&#x201D; heading and look at the message categories. Find the one called &#x201C;Inserting Text&#x201D; &#x2013; that looks like it&#x2019;s just what we&#x2019;re looking for &#x2013; and click it.
You&#x2019;ll see that the first message listed is the &#x201C;insertText:&#x201D; message &#x2013; again that looks like what we need. Click that.
You should see the window jump down to the documentation for the &#x201C;insertText:&#x201D; 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 &#x201C;open square bracket&#x201D;, object pointer name, message (with parameters), &#x201C;close square bracket&#x201D;.
Remove the NSLog statement from the displaySomeText method and replace it with the message we want to send to the text view:
[textView insertText:@&#x201D;displaySomeText just got called! &#x201D;];
The square brackets indicates to Objective-C that we are sending a message. Also note that we&#x2019;ve added a backslash-n to the end of the string object we&#x2019;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 &#x201C;Build and Run&#x201D;
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&#x2019;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&#x2019;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 &#x201C;Tasks selection in the middle left column. Click on the subheading &#x201C;Configuring Buttons&#x201D;, and select the &#x201C;setTitle:&#x201D; message on the right.
We see that the &#x201C;setTitle:&#x201D; 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&#x2019;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).
We also want to disable our button once we&#x2019;ve clicked it.
Open the documentation for the NSButton Class Reference again.
We don&#x2019;t see any message categories in the &#x201C;Tasks subheading that fit our needs. However, the NSButton class inherits behaviors from a number of super classes, including the NSControl class &#x2013; let&#x2019;s pull up the documentation for that class reference by clicking on the hyperlink in the NSButton Class Reference documentation for NSControl.
Expand the &#x201C;Tasks&#x201D; 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&#x2019;s look at the actions grouped under &#x201C;Enabling and Disabling the Control&#x201D;. Click this.
This message subcategory has two messages; the one that appears to meet our needs is &#x201C;setEnabled:&#x201D;. 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&#x2019;s send one more message to the button object:
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.
Storing Information in
• 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.
• 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
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.