objective-cxcodemacoscocoansbutton

NSButton Push On Push Off Type


I am new and just learning. I have seen many examples of NSSwitch buttons in iOS but I am having trouble getting a Push On / Push Off type button to work in MacOS Obj-C.

For a simple example:

When the program loads, a push on / push off style button is inactive (in default state). I have some other buttons that are also active.

When the push on / push off button type is pushed (active) I want to disable the other buttons and change a text label to say INACTIVE.

When the button is the pressed again (inactive) I want to enable the other buttons and change the text label again.

My code works on one press - meaning, I start the program and press the button and the other buttons disable and the text label changes.

However, the second press doesn't work. The button stops after one press and doesn't work as a "switch." I know with iOS switch buttons you can check isOn state, but I could only find an "isEnabled" on MacOS.

Any examples of a push on / push off style button states for MacOS Objective-C?

- (IBAction)playButton:(id)sender {
    if (self.playButton.isEnabled) {
        self.textLabel.title = @"INACTIVE";
        [self.Visual01Menu setEnabled:NO];
        [self.Visual02Menu setEnabled:NO];
        [self.Visual03Menu setEnabled:NO];
    }
    else {
        self.textLabel.title = @"ACTIVE";
        [self.Visual01Menu setEnabled:YES];
        [self.Visual02Menu setEnabled:YES];
        [self.Visual03Menu setEnabled:YES];
    }
}

Solution

  • What you're looking for is called state. You can compare whether the button in the following way:

    Objective-C

    self.playButton.state == NSControlStateValueOn  // or NSControlStateValueOff
    

    Swift

    self.playButton.state == .on  // or .off
    

    Enabled is for whether you can interact with the control at all, a disabled control won't receive user input.