Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Accessing NSAlert Buttons (http://www.mac-forums.com/forums/os-x-development-darwin/309156-accessing-nsalert-buttons.html)

Yodda_Hunter 03-02-2014 07:25 PM

Accessing NSAlert Buttons
 
I'm wondering,how to Write codes to the Buttons that appears at the Alert Message,as i want to do a specific action with these buttons.

Mark FX 03-05-2014 03:30 PM

The NSAlert's buttons return an integer, depending on which button is clicked, so you should use an if end if condition check like this.

Code:

NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Delete"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Delete the record?"];
[alert setInformativeText:@"Deleted records cannot be restored."];
[alert setAlertStyle:NSWarningAlertStyle];

if ([alert runModal] == NSAlertFirstButtonReturn) {
    // Delete button clicked.
} else {
    // Cancel button clicked.
}

Or.

Code:

NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Answer this question?"];
[alert setAlertStyle:NSWarningAlertStyle];

NSInteger result = [alert runModal];

if ( result == NSAlertFirstButtonReturn ) {
        // Yes button clicked
} else if ( result == NSAlertSecondButtonReturn ) {
        // No button clicked
} else if ( result == NSAlertThirdButtonReturn ) {
        // Cancel button clicked
}

If you use the class method.
+ (NSAlert *)alertWithMessageText:(NSString *)messageTitle defaultButton:(NSString *)defaultButtonTitle alternateButton:(NSString *)alternateButtonTitle otherButton:(NSString *)otherButtonTitle informativeTextWithFormat:(NSString *)informativeText
To create your alert panel.

Then you can check the NSAlertDefaultReturn = 1, NSAlertAlternateReturn = 0, NSAlertOtherReturn = -1, or NSAlertErrorReturn = -2 to check for an error, in the same way as the above examples.

Hope this helps.

Regards Mark


All times are GMT -4. The time now is 05:27 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.