Hitesh Bhardwaj
Hitesh Bhardwaj

Reputation: 73

Disable the NSAlert button when suppression button is checked

I have an NSAlert with setShowsSuppressionButton:YES for a checkbox and two buttons named OK and Cancel. How would I make the Cancel button to become disabled whenever we click(check) on the suppression button?

Upvotes: 2

Views: 1210

Answers (2)

Felix Seele
Felix Seele

Reputation: 857

You need to disable the button and set the action and target atttributes of the suppressionButton :

alert = [[NSAlert alloc] init];
[alert setMessageText:@"text"];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert.buttons[0] setEnabled:NO];
[alert setShowsSuppressionButton:YES];
[alert.suppressionButton setTitle:@"You need to activate me first"];
[alert.suppressionButton setTarget:self];
[alert.suppressionButton setAction:@selector(selectClicked:)];
[alert runModal];

In the action handler you can then toggle the enabled attribute of the button based on the state of the checkbox:

-(IBAction)selectClicked:(NSButton *)sender {
    for (NSView *view in sender.superview.subviews) {
        if ([view isKindOfClass:[NSButton class]]) {
            NSButton *button = (NSButton *)view;
            if ([button.title isEqualToString:@"OK"]) {
                button.enabled = (sender.state == NSControlStateValueOn);
            }
        }
    }
}

Upvotes: 1

rezand
rezand

Reputation: 576

set a boolean once the suppression button is active and check for that when creating the alert or check for it with a if else statement creating one of two different alerts, one with and one with out cancel.

Upvotes: 0

Related Questions