Online Book Reader

Home Category

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

By Root 810 0
in this case, a callout is shown with a proposed solution to the problem. Press Return to apply the proposed solution.

Figure 2.21. Compiling

Once your application is running, click the buttons and see the generated random numbers. Congratulations—you have a working Cocoa application.

Did you see the log statement on the console? When things go badly, the Cocoa classes will log to the console, so you will want to keep an eye on the console while testing your application. The console is part of the Debug Area. You can control console visibility by using the Debug Area toggle on the toolbar. By default, Xcode is configured to show the console only when log output is generated and to hide it when the program exits. You can configure Xcode to always show the log in the Behaviors tab of the Preferences panel, as shown in Figure 2.22.

Figure 2.22. Behaviors in Preferences

awakeFromNib


Note that your application is flawed: When the application starts, instead of anything interesting, the word Label appears in the text field. Let’s fix that problem. You will make the text field display the time and date that the application started.

As we discussed earlier, a NIB file is a collection of objects that have been archived. When the program is launched, the objects are brought back to life before the application handles any events from the user. This mechanism is a bit unusual; most GUI builders generate source code that lays out the user interface. Instead, Interface Builder allows the developer to edit the state of the objects in the interface and save that state to a file.

After being brought to life but before any events are handled, all objects are automatically sent the message awakeFromNib. You will add an awakeFromNib method that will initialize the text field’s value.

Add the awakeFromNib method to RandomController.m. For now, just type it in. You will understand it later on. Briefly, you are creating an instance of NSDate that represents the current time. Then you are telling the text field to set its value to the new calendar date object:

- (void)awakeFromNib

{

NSDate *now;

now = [NSDate date];

[textField setObjectValue:now];

}

The order in which the methods appear in the file is not important. Just make sure that you add them after @implementation and before @end.

You will never have to call awakeFromNib; it gets called automatically. Simply run your application again. You should now see the date and time when the app runs (Figure 2.23).

Figure 2.23. Completed Application

In Cocoa, a lot of things, such as awakeFromNib, get called automatically. Some of the confusion that you may experience as you read this book will come from trying to figure out which methods you have to call and which will get called for you automatically. We’ll try to make the distinction clear.

Documentation


Before this chapter wraps up, you should know where to find the documentation, as it may prove handy if you get stuck while doing an exercise later in the book. The easiest way to get to the documentation is by choosing Documentation and API Reference from Xcode’s Help menu (Figure 2.24).

Figure 2.24. Documentation and API Reference

If you Option-click on a method, class, or function name, Xcode will show a quick-help pop-over from which you can go to the full documentation for that term. Also, the Quick Help Inspector in the utility area displays quick help for the term under the cursor, or the selected object in Interface Builder.

What Have You Done?


You have now gone through the steps involved in creating a simple Cocoa application:

• Create a new project.

• Lay out an interface.

• Create custom classes.

• Connect the interface to your custom class or classes.

• Add code to the custom classes.

• Compile.

• Test.

Chronology of an Application


Let’s briefly discuss the chronology of an application: When the process is started, it runs the NSApplicationMain function, which creates an instance of NSApplication. The application

Return Main Page Previous Page Next Page

®Online Book Reader