Learn Cocoa
This tutorial shows you how to create your first Cocoa app. It assumes no previous
experience with programming or Xcode.
The app you will create contains no code, yet still has some impressive
functionality. It's a basic starting point only, but enough to get you going.
You must be using Xcode on Mac OS X 10.4 Tiger (
click here for Leopard version).
This
tutorial is written and illustrated by
Scott Stevenson
Copyright © 2006 Scott Stevenson
Create a New Project
First, Launch Xcode. If you have not installed Xcode, you can find it on your
Mac OS X installation CD or the disk that came with your Mac. The installer is
called Xcode Tools.
Once Xcode is running, open Preferences and set your Layout to All-in-One.
All-in-One is the layout preferred by many pros, and is
used in all of the visuals in this tutorial.
After you've changed the layout, close the Preferences window and choose
File → New Project
Choose Cocoa Application. Some of the items in the project list have similar
names, so choose carefully. Click the Next button, and you'll be asked to name
the project:
Name the project "CocoaNotepad". You can choose any location you like. It's good to put
all of your projects in one location, so ~/Development/CocoaNotepad might be a good
choice.
The Xcode Window
Once you create the project, the main Xcode window appears. The Xcode window
has several views you can switch between using the Pages toolbar item.
This is the Project View. It lists files, search results, build settings, and more.
Clicking the Build and Go toolbar item will compile your application and
run it.
There's a dropdown in the toolbar labeled Active Build Configuration, initially set to
Debug.
This dropdown controls build settings. You'd set this to Release and re-build if you want
to run your application outside of Xcode. Leave it set to Debug for now.
The Pages segmented control at the far left of the toolbar allows you to switch
between the Project, Build, and Debugging views.
Switch to the Build View by clicking the middle icon in the Pages toolbar item. The rest of
this tutorial will use this view exclusively.
Interface Builder
In the Build View, unfold the CocoaNotepad group, then the Resources folder.
Double-click the MainMenu.nib item to open it in Interface Builder.
Interface Builder is used by Cocoa programmers to lay out the user interface of
an application.
The Application Window and is where you place controls that the user will see.
The Menu displays the contents of your application's menu bar. You can
double-click menu items to rename them or simply select them to move or delete them.
The Object Palette contains stock items in Cocoa that you can add to your
application via drag-and-drop. Open the object palette now using
Tools → Palettes → Show Palettes.
The Document Window displays the contents of the MainMenu.nib file itself. The
user doesn't see this in the application — it's only used during development. (Keep
in mind that anyone can open an application's NIB file, though.)
Add a Text View
The items in the object palette can be dragged out and dropped
onto the application window. The icons along the top of
the palette represent groups of items. You can hover the mouse
over the groups and items to see their names.
The third toolbar icon from the left displays the Cocoa Text
group. Click on it to display the group contents, then
drag NSTextView to the application canvas.
Place the text view near the top-left of the window, then resize it using the
handles around the outside:
It should nearly fill the window, with a bit of
space on the bottom. The exact size is not important.
Finally, we need to set the sizing attributes for the text view so that
it keeps the same proportions as the window. Select the
text view and choose Tools → Show Inspector, then select
Size from the dropdown at the top of the inspector window.
The bottom half of the inspector window contains a section labeled
Autosizing. These are called "spring and struts."
Click the horizontal and vertical lines in the internal box so
that they change to springs. This
will cause the text view's internal size to grow and shrink with the window.
Add a Menu
Select the Cocoa Menus group in the object palette. In most cases, this
is the first group in the toolbar.
Drag the Format menu item from the palette over to the application menu bar and
drop it between Edit and Window.
Save MainMenu.nib by pressing ⌘-S. If you are asked to select a
format, choose the 10.2 and later option.
Run the Application
Switch back to Xcode, then build and run the application by pressing
⌘-R, or by simply choose Build → Build and Run.
Even though this project doesn't have a single line of custom code, you can see that
we already have a working application. Choose Format → Font → Show Fonts
to choose a font and start typing.
It can't save files, but it uses
Cocoa's built-in text view so you can use fonts, colors, international text,
and spell checking. It also support drag-and-drop, text search, and many
other features.
Printing and PDF
The application also supports printing and can create PDF files. To try this out,
simply choose File → Print. Click Preview
in the print dialog to convert the text to a PDF.
This document can be treated as any other PDF document, including text selection,
searching and annotation.
Wrap Up
The goal of this tutorial is just to give you a taste of what Cocoa has
to offer. Even though we didn't write any code, we ended up with an application
which has some very sophisticated text handling. The Xcode project is contained
in the following zip file.
If you'd like to see more tutorials like this, please make a donation.
Please consider the download a
thank you gift for your contribution.
Perfection and power are overrated
I think you were wise to choose happiness and love