Chapter 1 to 5
Upcoming SlideShare
Loading in...5
×
 

Chapter 1 to 5

on

  • 695 views

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

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

Statistics

Views

Total Views
695
Views on SlideShare
695
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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. <br /> <br /> 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. <br />
  • 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. <br />
  • You&#x2019;ll see a standard Mac OS X save window. We&#x2019;re going to call this project &#x201C;TextApp&#x201D;. <br /> <br /> 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. <br />
  • Now you can see the Xcode project window with the template files for your project. <br /> <br /> 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. <br /> <br /> 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. <br /> <br />
  • 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;. <br /> <br /> * Comments are shown in green. <br /> * Precompiler directives are shown in brown. File names that are part of the precompiler directives are shown in orange. <br /> * Keywords are shown in fuschia. <br /> * Cocoa class names, message calls and method names are shown in purple. <br /> * Other colors are used to identify other key elements. Elements that don&#x2019;t fall into a specific category are shown in black text. <br /> <br /> 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. <br /> <br /> 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. <br /> <br /> 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. <br />
  • 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. <br /> <br /> You&#x2019;ll see a number of windows on your screen. <br />
  • 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. <br />
  • 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. <br /> <br /> Click the &#x201C;TextApp&#x201D; menu to display it, and double-click the first menu item. You can then edit the contents. <br /> <br /> Change it to &#x201C;About My Wonderful TextApp Application&#x201D;. <br /> <br /> When you&#x2019;re finished, save the MainMenu.xib file to save your menu changes. <br /> <br /> From the Tools menu, open the Library Pallette. <br />
  • You&#x2019;ll see a number of built-in controls that are part of the Cocoa framework in this palette. <br /> <br /> At the bottom of the Library palette window is a search box; type &#x201C;text view&#x201D; into this box to narrow your selection. <br />
  • 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. <br /> <br /> 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. <br />
  • You should have something that looks similar to this. <br /> <br /> Save your Interface Builder file, and switch back to Xcode. <br /> <br /> 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. <br /> <br /> Now we&#x2019;re going to write some code to go with our application. <br />
  • 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;. <br />
  • The New File window should appear. <br /> <br /> 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;. <br /> <br /> 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;. <br />
  • 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. <br /> <br /> 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. <br /> <br /> 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. <br />
  • Pull up the Library Palette (&#x201C;Tools >> Library&#x201D;) and type &#x201C;object&#x201D; in the search box. <br /> <br /> Click on the &#x201C;Object&#x201D; selection and drag and drop it into the MainMenu.xib window. <br /> <br /> Select the new &#x201C;Object&#x201D; icon and click the &#x201C;Inspector&#x201D; icon at the top of the MainMenu.xib window. <br />
  • 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. <br /> <br /> Save your MainMenu.xib file in Interface Builder and return to Xcode. <br />
  • Now we&#x2019;re going to write the method that gets executed when our application gets launched. <br /> <br /> Open the NotifyingClass.m file. <br /> <br /> 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;. <br /> <br /> Scroll down and find the relevant lines that define the NSObject interface. Copy the line &#x201C;- (id)init;&#x201D; to your clipboard. <br /> <br />
  • Switch back to the &#x201C;NotifyingClass&#x201D; implementation file and paste the line between the @implementation and @end lines. <br /> <br /> Remove the semicolon and replace it with a pair of curly brackets. <br />
  • Add a line that logs a message to the Console. <br /> <br /> Type in &#x201C;NSLog(@&#x201D;Hello World! I&#x2019;m a new NotifyingClass instance!&#x201D;); <br /> <br /> followed by <br /> <br /> return self; <br /> <br /> 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. <br /> <br /> 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. <br /> <br /> 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. <br /> <br /> Return to Xcode, run your application again, click the &#x201C;Stop Tasks&#x201D; icon and pull up the console. <br />
  • You should see two messages logged into the console, one from each instance of the NotifyingClass object. <br /> <br /> This carries us through the end of Chapter 3. <br />
  • Now we&#x2019;re going to change our NotifyingClass method to actually send a message to the TextApp text window. <br /> <br /> Click on the NotifyingClass header file to display the code, and add a signature as follows: <br /> <br /> - (void)displaySomeText; <br /> <br /> to identify a new method. <br /> <br /> 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. <br /> <br /> 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. <br /> <br /> Save your implementation file and switch back to the header file. <br />
  • 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. <br /> <br /> 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;. <br /> <br /> Save your header file and switch to the NotifyingClass implementation file. <br />
  • Make the same changes to the method signature, and save the implementation file. <br /> <br /> Double click the MainMenu.xib file name in the left hand pane to pull up Interface Builder. <br /> <br /> <br />
  • 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. <br />
  • 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. <br /> <br /> 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. <br />
  • 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. <br /> <br /> 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... <br />
  • 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. <br /> <br /> Open the NotifyingClass header file in Xcode. <br />
  • 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. <br /> <br /> Add &#x201C;NSTextView *textView;&#x201D; between the curly braces as shown. <br /> <br /> 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. <br /> <br /> Save the header file and switch to Interface Builder by clicking on the MainMenu nib file name in the left hand panel. <br />
  • 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. <br /> <br /> Link this outlet to the text view object in the TextApp window. <br /> <br /> Save your work in Interface Builder and switch back to Xcode. <br /> <br /> 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. <br />
  • 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. <br /> <br /> 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. <br />
  • You should see the window jump down to the documentation for the &#x201C;insertText:&#x201D; message. <br /> <br /> insertText: takes one attribute, a string object. Excellent! <br /> <br /> Now that we know what message to send to interact with the text view object, close the documentation and open the NotifyingClass implementation file. <br />
  • 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;. <br /> <br /> Remove the NSLog statement from the displaySomeText method and replace it with the message we want to send to the text view: <br /> <br /> [textView insertText:@&#x201D;displaySomeText just got called! <br /> &#x201D;]; <br /> <br /> 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. <br /> <br /> Save your changes in Xcode, and click &#x201C;Build and Run&#x201D; <br />
  • 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. <br /> <br /> 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. <br /> <br /> First let&#x2019;s look up the documentation for the NSButton Class Reference. <br />
  • 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. <br />
  • We see that the &#x201C;setTitle:&#x201D; message takes one parameter, a string object, and does not return anything to the message sender. <br /> <br /> Close the documentation and pull up the NotifyingClass implementation file. <br /> <br />
  • 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). <br /> <br /> [sender setTitle:@&#x201D;Clicked!&#x201D;]; <br /> <br /> We also want to disable our button once we&#x2019;ve clicked it. <br /> <br /> Open the documentation for the NSButton Class Reference again. <br />
  • 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. <br />
  • 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. <br />
  • 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. <br />
  • OK. This is it. <br /> <br /> 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. <br /> <br /> Close the documentation and switch back to Xcode to the NotifyingClass implementation source code <br />
  • Let&#x2019;s send one more message to the button object: <br /> <br /> [sender setEnabled:NO]; <br /> <br /> Save your changes and click Build and Run. <br />
  • This time, you can only click the button once, and send one message to the text view object. <br /> <br /> This wraps up Chapter 4. <br />
  • <br />
  • At this point we need to switch to the Objective-C documentation slides. <br />

Chapter 1 to 5 Chapter 1 to 5 Presentation Transcript

  • The Project Window
  • (1) (2) (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.
  • 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.