Information Technology Reference
In-Depth Information
After you have completed Step 2, you can continue to develop and debug your custom code and the new code
will be active in your UI.
Any IB object can be subclassed; you can create completely new or substantially modified buttons, sliders, map
views, Web views, and so on.
In practice, the UIView class in iOS and the NSView class in OS X are subclassed regularly, because it's often
useful to add custom graphics or event management code to a view. Other controls and views are subclassed
less frequently.
TIP
You can subclass any object in a nib. It doesn't have to be visible or part of the UI.
CAUTION
Customizing Apple's own subclasses of UIView and NSView —map views, Web views, image views, and so
on—can be a frustrating experience. The more complex subclasses include undocumented features and properties,
and it's often impossible to add new features without tricks and workarounds. For example, in some situations, an-
imated view swaps are handled by “invisible” views that aren't part of the officially documented view system. Al-
ways check unofficial online developer forums to see if other developers have experienced problems—and if they
may have already solved them.
Adding a new subclass in Xcode
In this example, we add a new subclass of UIView to the IB project from Chapter 7 and assign the subclass to
the background view of the project.
Begin by copying and pasting the project folder in Finder. Rename the folder. The new name is arbitrary, but it
must be unique. In the sample code for this project, the new name is IB Custom View. Open the folder, and
double-click the .xcodeproj file to load it into Xcode 4.
CAUTION
The project file is still called IB.xcodeproj . This doesn't matter. As explained in Chapter 2, it's very difficult to
rename the working files used in an Xcode project, and it isn't usually necessary to do so.
When the project loads, right-click the Classes group and select New File… Select the Cocoa Touch templates,
and then select the Objective-C Class template. Select UIView in the Subclass menu, as shown in Figure 8.4.
Save the file as IBView .
FIGURE 8.4
Adding a new subclass of UIView to an iOS project
Search WWH ::




Custom Search