Subclassing in IB

This section includes critical information about linking objects in your app to objects on the screen. Whenever you subclass an onscreen object, you must tell IB to use the subclass. You must do this manually.

caution_exclamation_2c.eps

Until you get into the habit of subclassing in IB, it’s easy to forget this step and then wonder why your new code does nothing. It’s useful to create a sanity list of basic checks for development, and it’s a very good idea to include subclassing in IB on the list.

Let’s see what this means in practice. We’ll create another sample project using the SingleView Application template, subclass the view that’s included, and add some code to it. We’ll see that IB ignores the code—a very bad thing—until we tell it to display our subclass instead of the default UIView class.

Follow these steps:

1. Create a new sample project using the SingleView Application template, following the steps in Chapter 3. Save it as IBSubclassing, although the name isn’t critical.

2. Follow the instructions in Chapter 6 to add a new subclass of UIView to the project. Save it as MyView, although again, the name isn’t critical.

caution_exclamation_2c.eps

Make sure you select UIView from the class list in the New File dialog box, and not UIViewController.

3. Add a touchesBegan: method that logs touches ...

Get iOS App Development Portable Genius now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.