Disable a IBAction (id) sender button programatically in Xcode while developing app for iOS & OSX

While developing apps for OSX or iOS you will be wondering how to disable IBAction button programatically.

 
Here is how to do it.

 

Assumption: I assume you are using Storyboards for developing your apps within Xcode.

 
Step 01: Drag UIButton (iOS) or NSButton (OSX) on Storyboard. I Also assume that you have dragged three buttons on storyboard & you have named it as Start, Pause & Reset.

 
Step 02: Now create IBAction for that button in .h header file or directly in .m method file now it will look something like this.

 

-(IBAction)startIBA:(id)sender {
}

-(IBAction)pauseIBA:(id)sender {
}

-(IBAction)resetIBA:(id)sender {
}

 
Step 03: Now create IBOutlet for same three buttons in .h header file. It will look something like this

 

// Use This code for OSX App
@property (strong) IBOutlet NSButton *pauseEnable;
@property (strong) IBOutlet NSButton *startEnable;
@property (strong) IBOutlet NSButton *resetEnable;

// Use this code for iOS App
@property (strong, nonatomic) IBOutlet UIButton *pauseEnable;
@property (strong, nonatomic) IBOutlet UIButton *startEnable;
@property (strong, nonatomic) IBOutlet UIButton *resetEnable;

 

Note: to create IBAction & IBOutlet for particular button you have control drag from respective button connected header file.

 
Step 04: So now here is how will you do it to disable button at tap or click or viewDidLoad.

ViewDidLoad Code will look something like this. You will put code here if you want to disable button at startup.

 

- (void)viewDidLoad {
    NSLog(@"viewDidLoad");
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    
    self.pauseEnable.enabled = NO; // Pause button disabled at startup
    self.resetEnable.enabled = NO; // Reset button disabled at startup
}

 
Step 05: Here is how you will do it to disable button by tapping other button.

 
Situation A: After tapping Start Button, Pause & Reset button should get enabled. Your code will look something like this.

 

-(IBAction)startIBA:(id)sender {
    _pauseEnable.enabled = YES; // Pause button enabled
    _resetEnable.enabled = YES; // Reset button enabled
}

 
Situation B: After tapping Pause button pause itself should get disabled & start & reset button should get enabled.

 

-(IBAction)pauseIBA:(id)sender {
    _pauseEnable.enabled = NO; // Pause button disabled
    _startEnable.enabled = YES; // Start button enabled
    _resetEnable.enabled = YES; // Reset button enabled

}

 
Situation C: After tapping reset button reset button itself should get disabled, Pause button should also get disabled & start button should get enabled.

 

-(IBAction)resetIBA:(id)sender {
    _resetEnable.enabled = NO; // Reset button disabled
    _pauseEnable.enabled = NO; // Pause button disabled
    _startEnable.enabled = YES; // Start button enabled
}

 
Hope it helps,

 
Thanks & Regards
Mandar Apte

One thought on “Disable a IBAction (id) sender button programatically in Xcode while developing app for iOS & OSX

  1. can you convert your c-objective in swift

    – (void)viewDidLoad {
    NSLog(@”viewDidLoad”);
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.pauseEnable.enabled = NO; // Pause button disabled at startup
    self.resetEnable.enabled = NO; // Reset button disabled at startup
    }

Leave a Reply

Your email address will not be published. Required fields are marked *