2010-05-27 8 views

Respuesta

41

Tome un vistazo a NSAlert, que tiene una sincrónica -runModal método:

NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
[alert setMessageText:@"Hi there."]; 
[alert runModal]; 

Como Pedro menciona, una mejor alternativa es utilizar la alerta as a modal sheet en la ventana, por ejemplo:

[alert beginSheetModalForWindow:window 
       modalDelegate:self 
      didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) 
       contextInfo:nil]; 

Los botones se pueden agregar a través de -addButtonWithTitle::

[a addButtonWithTitle:@"First"]; 
[a addButtonWithTitle:@"Second"]; 

El código de retorno que indica qué botón se pulsa: ha pasado

- (void) alertDidEnd:(NSAlert *)a returnCode:(NSInteger)rc contextInfo:(void *)ci { 
    switch(rc) { 
     case NSAlertFirstButtonReturn: 
      // "First" pressed 
      break; 
     case NSAlertSecondButtonReturn: 
      // "Second" pressed 
      break; 
     // ... 
    } 
} 
+3

Aún mejor, comience la alerta como una hoja en la ventana que contiene el botón de borrar . De esta forma, el usuario puede continuar usando cualquier otra ventana en su aplicación. –

+0

Wow funciona bien. Pero cómo poner más botones en esta alerta y cómo obtener esos botones eventos – mikede

+1

@mik: Al usar ['-addButtonWithTitle:'] (http://developer.apple.com/mac/library/ documentation/cocoa/conceptual/Dialog/Tasks/UsingAlerts.html). También hay un [especial sobre alertas] (http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Dialog/Tasks/UsingAlerts.html) en los documentos que deberían ayudarlo. –

6

tiempo largo ya que la respuesta aceptada y las cosas han cambiado:

  • Swift se está volviendo más y más popular.
  • beginSheetModalForWindow(_:modalDelegate:didEndSelector:contextInfo:) está en desuso, deberíamos usar beginSheetModalForWindow:completionHandler: en su lugar.

reciente ejemplo de código en Swift:

func messageBox() { 
    let alert = NSAlert() 
    alert.messageText = "Do you want to save the changes you made in the document?" 
    alert.informativeText = "Your changes will be lost if you don't save them." 
    alert.addButtonWithTitle("Save") 
    alert.addButtonWithTitle("Cancel") 
    alert.addButtonWithTitle("Don't Save") 
    alert.beginSheetModalForWindow(window, completionHandler: savingHandler) 
} 

func savingHandler(response: NSModalResponse) { 
    switch(response) { 
    case NSAlertFirstButtonReturn: 
     println("Save") 
    case NSAlertSecondButtonReturn: 
     println("Cancel") 
    case NSAlertThirdButtonReturn: 
     println("Don't Save") 
    default: 
     break 
    } 
} 

En caso de que quiera una versión sincrónica:

func messageBox() { 
    let alert = NSAlert() 
    alert.messageText = "Do you want to save the changes you made in the document?" 
    alert.informativeText = "Your changes will be lost if you don't save them." 
    alert.addButtonWithTitle("Save") 
    alert.addButtonWithTitle("Cancel") 
    alert.addButtonWithTitle("Don't Save") 
    let result = alert.runModal() 
    switch(result) { 
    case NSAlertFirstButtonReturn: 
     println("Save") 
    case NSAlertSecondButtonReturn: 
     println("Cancel") 
    case NSAlertThirdButtonReturn: 
     println("Don't Save") 
    default: 
     break 
    } 
} 
+0

¡Me salvaste el día! – cmcromance

Cuestiones relacionadas