views:

319

answers:

1

For quickly mocking up UI, I'd like to be able to drag buttons onto a view in interface builder, then drag a connection from that button to the view that should appear when you click it.

A subclass of UIButton is a little inconvenient to use in IB, so I'd prefer to add the behavior to UIButton itself. Unfortunately, it seems like outlets created in a category aren't visible in IB:

@interface UIButton (myextensions) {
    IBOutlet UIView *outletDestination;
}
@end

Can extra outlets be added this way?

+1  A: 

Can extra outlets be added this way?

Nope. You can't add instance variables by declaring them in a category.

You can, however, add properties, and you can put IBOutlet on properties, so you can add outlets that way. With the modern runtime (the only one available on the iPhone), properties can add instance variables.

I don't think you can do this if you intend to make a custom accessor for the property (you must use @synthesize), but it doesn't sound like this matters for your case: You're just mocking up UI, so you're not going to write custom accessors.

Alternatively, you can create outlets in IB itself on the Classes tab of the Library panel. Select a class there, then the Outlets tab in the pane below, then add an outlet to the list.

You'll need to have a nib open for that other solution, or no classes will show up. That's because it's context-sensitive: A Mac nib will have AppKit classes (like NSButton), whereas an iPhone nib will have UIKit classes.

Peter Hosey
If I add a @property to a UIButton category, I can't use @synthesize because it's a category, and I can't write my own methods because there's no ivar to store the data in. If I create the outlet in IB, "[<UIRoundedRectButton 0x391d5f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key destination." I tried all the above with UIRoundedRectButton, too.
alltom
Adding outlets in IB is only really useful for mocking up; when you get down to actually programming, you can and should declare the outlets in the headers. What do you mean “I can't use `@synthesize`”? What happens when you try?
Peter Hosey
I'm sorry I missed your comment; I don't think I got an e-mail notification. The error for using @synthesize is this: "@synthesize not allowed in a category's implementation"
alltom
alltom: Huh. You're right. Sorry for the wrong path. I guess you'd have to implement a custom accessor using associative references in place of ivars, but the documentation seems to suggest that that feature is not available in Cocoa Touch yet. http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/articles/ocAssociativeReferences.html
Peter Hosey
That would be what I want! Ah, well.
alltom