Search code examples
objective-cibactionnsbutton

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 setEnabled: NO];
    [stay setEnabled: NO]; // Using/altering "Stay's" button
}

Thanks in advance!


Solution

  • 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.