Adding outlets to UIButton
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 subclas开发者_开发知识库s 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?
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.
精彩评论