开发者

How to use an IBAction button outside it's scope

I have three IBActions, each linked with it's own button like so:

#import "Controller.h"
#import "Application.h"

@implementation Controller
- (IBAction)deal:(id)sender {
    NSButton *deal = (NSButton *)sender;
    ...
}

- (IBAction)hit:(id)sender {
    NSButton *hit = (NSButton *)sender;
    ...
}

- (IBAction)stay:(id)sender {
    NSButton *stay = (NSButton *)sender;
    ...
}
@end

How do you use/call on a button outside it's scope? I'm looking to do something like this:

 - (IBAction)hit:(id)sender {
    NSButton *hit = (NSButton *)sender;
    ...
    [hit se开发者_开发知识库tEnabled: NO];
    [stay setEnabled: NO]; // Using/altering "Stay's" button
}

Thanks in advance!


IBAction is a typedef of void, used to flag up methods that you want Interface Builder (or Xcode 4's interface designer to spot. They're just ordinary methods with no outward link to anything.

What you probably want is some IBOutlets to connect out to the button. E.g. to connect to the hit button, you'd add this to your @interface:

IBOutlet NSButton *hit;

Then in Interface Builder or Xcode you should be able to control-drag a link from your class out to the button and connect the outlet — the opposite of what you've been doing to connect the button events to your class, effectively.

Because hit is an instance variable, you can access it from any method in your class.


You can declare a NSButton property as IBOutlet in your view controller and then in the Interface Builder you can link your button with it.

Something like:

@property (nonatomic, assign) IBOutlet NSButton * button;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜