New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Accessing NSAlert Buttons


Post Reply New Thread Subscribe

 
Thread Tools
Yodda_Hunter

 
Member Since: Jun 08, 2013
Location: Būr said,Egypt
Posts: 158
Yodda_Hunter is on a distinguished road
Mac Specs: MacBook Pro (i5)

Yodda_Hunter is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks

Post Reply New Thread Subscribe


« Custom NSCell drawWithFrame:inView method not getting invoked in OSX 10.8 | Multiple Images Falling Issues on XCODE »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
15" MBP Keyboard buttons hadrian Apple Notebooks 1 09-27-2010 12:37 AM
How to use the keyboard to select buttons in dialogs (e.g., Don't Save/Cancel/Save) remain Switcher Hangout 7 07-11-2007 05:06 PM
Configure MX1000 Laser Mouse Buttons? cubbie5150 Switcher Hangout 1 03-11-2007 06:24 AM
Mac Buttons? MacGuy18 OS X - Operating System 2 03-21-2006 12:36 PM

All times are GMT -4. The time now is 03:30 AM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?