Interface Builder 3 Change: No Subclass Creation
January-16-2008 Filed in: cocoa | Interface Builder | Xcode
Older versions of Interface Builder let you create subclasses for Cocoa applications and generate source code files for the subclasses you created. As an example, you could drag an OpenGL view to a window, subclass the OpenGL view, and have Interface Builder create .h and .m files for the subclass. But Interface Builder 3 no longer lets you create subclasses. How do you create the subclasses?
You create the subclasses the old-fashioned way: with source code. Create your subclasses in Xcode. Interface Builder and Xcode are synchronized so the subclasses you create in Xcode appear in Interface Builder as well.
Choose File > New File to create a new file. A window will open containing many file types for you to choose from. For a Cocoa program you would want one of the Cocoa files unless you’re writing a Cocoa program in Ruby, in which case you would use one of the Ruby files. Name your file and click the Finish button. You’ve created a subclass file.
Instantiating Your Class
After creating your class in Xcode, you may need to add an instance of your class to the nib file so you can make connections in Interface Builder. If Interface Builder’s library window is not open, open it by choosing Tools > Library. The library window contains Interface Builder’s user interface elements.
Select an NSObject from the library window. NSObject is a blue cube. Drag NSObject to the nib file window. An NSObject named Object should now appear in the nib file window. Choose Tools >Identity Inspector to open the identity inspector. Select the NSObject in the nib file window.
The top of identity inspector should have a combo box labeled Class. Choose your class from the list of classes in the combo box. The name of the NSObject instance in the nib file window will change from Object to the name of your class. Any outlets and actions in your class should appear in the identity inspector.
If your classes are not appearing in Interface Builder, choose File > Synchronize With Xcode in Interface Builder.
7 Responses to “Interface Builder 3 Change: No Subclass Creation”
Leave a Reply
Powered by WordPress


It’s certainly true that the main expected way to define classes is to just create them in an Xcode project, rather than to create them in Interface Builder. But you can create classes in Interface Builder 3 just fine. It’s just different from the way it was done in Interface Builder 2, not non-existent.
Drag an item from the Library — which even has a bare “NSObject” in it now — and open that object’s Identity Inspector pane. You can set the object’s class to anything you want, even a class you haven’t defined in your project yet. (That’s why the class is represented by a combo box, rather than by a pop-up menu.)
You can add outlets and actions to the class in the Identity Inspector as well, and even — from the File menu — write out the declaration for the class as Objective-C, Python or Ruby code.
I’m very interested in coding but when I read this article i kind of realised how far I am behind. I’m still to get my head around OOP so I’d better shoot off and come back later.
If you do use the new IB to generate the 2 files for your class – after you have ‘saved’ in IB, you’ll have to go into your app/classes in xcode and file-add-existing and add them both.
Then edit the .h and specify the superclass. (I couldn’t find, in IB, where you could specify this once you created the new object and then the class for that object).
I can't seem to open the library window in InterfaceBuilder (via the Tools->Library menu, or Shift-Command-L) — nothing happens.
Also, Find in Library doesn't bring up a window, and there is no About dialog!
Anybody have any ideas?
Andy,
The first thing I would do is trash Interface Builder’s preferences file and restart Interface Builder. You can find it at the following location:
/Users/YourUsername/Library/Preferences
If that doesn’t work, reinstall Xcode. If reinstalling Xcode doesn’t fix the problem, post your question on Apple’s Xcode mailing list. Apple’s Xcode and Interface Builder engineers read the mailing list, and they would know more than anyone how to solve your problem.
Thanks, Mark.
It turned out to be a case of “equipment superior to operator”.
I have an LCD monitor attached to my laptop, which it shares with another computer — the library window was showing up there. Since I normally use the monitor for the other computer, I couldn’t see it! So it looked to me like nothing was happening.
I appreciate the help though,
Andy
Actually, at least in IB 3.2.2, you can click on the little gear in the bottom left corner of the Library window, and if you have one of the classes selected above (click the classes tab and pick something), one of the options is “New Subclass…” Hope that helps. Now, to learn how to make connections using this new IB…