Question

I have added two buttons to the NSAlert object, at the moment the return code for button one is 1001, and for button to by default its 1000. I need to determine effectively which button in a given alert is pressed without working with magic numbers. It gets messy otherwise.

-(void)showErrorMessage:(NSString*)errorMessage{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:@"Error"];
    [alert setInformativeText:errorMessage];
    [alert setAlertStyle:NSCriticalAlertStyle];
    [alert addButtonWithTitle:@"Retake test"];
    [alert addButtonWithTitle:@"Cancel test"];
    [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(retakeFingerPrintAlert:returnCode:contextInfo:) contextInfo:nil];
}

- (void)retakeTestAlert:(NSAlert *)alert
                    returnCode:(int)returnCode
                   contextInfo:(void *)contextInfo{
    NSLog(@"clicked %d button\n", returnCode);


    //I want to determine very clearly which button is being pressed in the NSAlert
    //I dont want to work with magic numbers

    //And thus call the below method dependng on the button clicked
    [self onRetakeTest];
}
Was it helpful?

Solution

According to Apple's documentation on "addButtonWithTitle:", which you're using:

The first three buttons are identified positionally as NSAlertFirstButtonReturn, NSAlertSecondButtonReturn, NSAlertThirdButtonReturn in the return-code parameter evaluated by the modal delegate. Subsequent buttons are identified as NSAlertThirdButtonReturn +n, where n is an integer

So the first button should equal [NSAlertFirstButtonReturn], which translates to "1000". The fourth button should be "1003".

Makes sense so far?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top