Results 1 to 2 of 2
  1. #1
    Accessing NSAlert Buttons

    Member Since
    Jun 08, 2013
    Location
    Būr said,Egypt
    Posts
    158
    Specs:
    MacBook Pro (i5)
    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.

  2. #2
    Accessing NSAlert Buttons
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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 *)alertWithMessageTextNSString *)messageTitle defaultButtonNSString *)defaultButtonTitle alternateButtonNSString *)alternateButtonTitle otherButtonNSString *)otherButtonTitle informativeTextWithFormatNSString *)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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Expose Buttons as Back Buttons?
    By jebug29 in forum OS X - Operating System
    Replies: 1
    Last Post: 06-07-2015, 10:38 PM
  2. NAvigation between buttons
    By B-ram in forum iOS and Apps
    Replies: 0
    Last Post: 10-20-2010, 06:27 AM
  3. Colors on buttons..
    By rehless in forum Switcher Hangout
    Replies: 2
    Last Post: 10-03-2009, 04:38 PM
  4. F buttons
    By Ari in forum Schweb's Lounge
    Replies: 1
    Last Post: 12-13-2008, 04:36 PM
  5. Replies: 7
    Last Post: 08-31-2006, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •