Information Technology Reference
In-Depth Information
NOTE
Because this isn't a primer on Cocoa Touch, the code is very simple. For more information about app design, see
the companion Cocoa Touch in Wiley's Developer Reference series.
These are the basic rules for UI design:
When you want to read or write an object's properties from the UI, you must add the corresponding
class to your code. For example, if the UI includes a label, you must use IB to add a UILabel object to your
code.
The code is usually placed inside a view controller object, and the object that appears in the display is in-
cluded in the corresponding view nib file. This isn't an absolute rule—other arrangements are pos-
sible—but it's a typical starting point.
After you have code for a UI object you can define an outlet that links them together. When your code
changes an object's properties (its size, position, text label, or some other attribute), the link copies the
change to the UI manager and the visible representation of the object is updated on the display.
If you want your app to respond to user events, you must define an action for each event. An action is a
special method added to the code to handle user events.
UI objects can support outlets and actions at the same time.
You can create “dumb” objects that support neither outlets nor actions. Use dumb objects to decorate the
UI—for example, to add a fixed graphic as a background to a view. A dumb object doesn't respond to the
user, and it can't be changed from your code.
NOTE
Technically, actions rely on the Target-Action model in Cocoa and Cocoa Touch. You can define actions in IB, but
you can also define them in code, and for advanced effects, you can redefine them dynamically. A full discussion
of Target-Action is outside the scope of this topic. For details, see the Cocoa companion title in Wiley's Developer
Reference series.
In previous versions of Xcode, objects, outlets, and actions were added manually. In Xcode 4, the process is
semi-automated, and it's much simpler, faster, and more productive than it used to be. It's also more reliable,
because the automation eliminates many common errors.
Adding objects to a view
Using the IB project from earlier in this chapter, arrange the Xcode interface as shown in Figure 7.13. Select the
IBViewController.xib object for editing, and click the Assistant editor to split the window as shown in
the figure. The editor shows the default view, which is the default UI that appears when the app runs.
The Assistant should show the IBViewController.h header file. If it doesn't, select it manually. In the bot-
tom right of the Utility area, select the Object library by clicking the box icon. If the icons are at the bottom of
the area, drag the divider up to show the contents of the library.
FIGURE 7.13
Search WWH ::




Custom Search