Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [9]

By Root 779 0
This is the project tab of the navigator; other navigator tabs show such information as compiler errors or find results. For now, you will be dealing with editing files, so expand the item that says Random to see the files that will be compiled into an application.

The skeleton of a project that was created for you will compile and run. It has a menu and a window. Click on the Run toolbar item to build and run the project, as shown in Figure 2.4.

Figure 2.4. Skeleton of a Project

While the application is launching, you will see a bouncing icon in the dock. The name of your application will then appear in the menu. This means that your application is now active. The window for your application may be hidden by another window. If you do not see your window, choose Hide Others from the Random menu. You should see an empty window, as shown in Figure 2.5.

Figure 2.5. Running the Project

Although it doesn’t do much, your application is already fully functional. Even printing works. There is exactly one line of code in the application. Let’s look at it now; quit Random and return to Xcode.

The main Function


Expand Supporting Files and select main.m by single-clicking on it. The code will appear in the editor (Figure 2.6). If you double-click on the filename, it will open in a new window. Because we deal with many files in a day, this tends to overwhelm us rather quickly, so we use the single-window style.

Figure 2.6. main() Function

You will almost never modify main.m in an application project. The default main() simply calls NSApplicationMain(), which loads and runs the objects that make up your application. In the next section, we will learn how NSApplicationMain() knows which objects to load.

In Interface Builder


In the project navigator under Random, you will find a file called MainMenu.xib. Click on it to open it in the Interface Builder editor. Next, click the Utilities view toggle in the toolbar to show the right-side panel (Figure 2.7).

Figure 2.7. MainMenu.xib

Interface Builder allows you to create and edit user interface objects, such as windows and buttons, for use in your application. You can also create instances of your custom classes and make connections between those instances and the standard user interface objects. When users interact with the user interface objects, the connections you have made between them and your custom classes will cause your code to be executed. Interface Builder saves these objects and their connections to a XIB (pronounced “zib”) file.

The Utility Area


The utility area has two panels: the Inspector and the Library. The Inspector panel contains settings for the currently selected file or Interface Builder object. The Library panel contains file templates, snippets, objects, and media that can be used in your project. User interface widgets can be dragged from the object library into your interface. For example, if you want a button, you can drag it from the object library area.

The Blank Window


Click on the window icon in the Interface Builder dock. The blank window that appears represents an instance of the NSWindow class that is inside your XIB file (Figure 2.8).

Figure 2.8. NSWindow Instance

As you drop objects from the library onto the window, they will be added to the XIB file. After you have created instances of these objects and edited their attributes, saving the XIB file is like “freeze-drying” (or archiving) the objects into the file. When our Random application runs, NSRunApplication() unarchives the objects we created in the XIB and brings them back to life. A more complex application would likely have several XIB files that are loaded as needed.

Once your application has loaded the objects, it simply waits for the user to do something. When the user clicks or types, your code will be called automatically. If you have never written an application with a graphical user interface before, this change will be startling to you: The user is in control, and your code simply reacts to what the user does.

Return Main Page Previous Page Next Page

®Online Book Reader