开发者

Adding outlets to UIButton

开发者 https://www.devze.com 2022-12-24 16:11 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号