Posted By

tylerhall on 08/14/07


Tagged

cocoa alert nsalert sheet


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

MrYAMANE
strangesthings
snowdrop


Run an NSAlert Sheet in your Cocoa App


 / Published in: Objective C
 

This code runs an alert sheet in your Cocoa app. You first create an NSAlert object, its properties, and define the method that should be called when the user dismisses the sheet. Then write the corresponding handler that tests for an "OK" or "Cancel" button press.

  1. NSAlert *alert = [[[NSAlert alloc] init] autorelease];
  2. [alert addButtonWithTitle:@"OK"];
  3. [alert addButtonWithTitle:@"Cancel"];
  4. [alert setMessageText:@"Sheet Title"];
  5. [alert setInformativeText:@"Message text goes here."];
  6. [alert setAlertStyle:NSWarningAlertStyle];
  7. [alert beginSheetModalForWindow:mainWindow modalDelegate:self didEndSelector:@selector(someMethodDidEnd:returnCode:contextInfo:) contextInfo:nil];
  8.  
  9. - (void) someMethodDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
  10. {
  11. if(returnCode == NSAlertFirstButtonReturn)
  12. {
  13. // Do something
  14. }
  15. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: davidsi on March 16, 2010

don't forget to release the alert at the end of the method.

You need to login to post a comment.