Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i implement a message box in a Cocoa application?

Tags:

cocoa

I have implemented delete functionality in cocoa application now i want to show one message box when user click on delete button.

like image 783
mikede Avatar asked May 27 '10 09:05

mikede


2 Answers

Take a look at NSAlert, which has a synchronous -runModal method:

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

As Peter mentions, a better alternative is to use the alert as a modal sheet on the window, e.g.:

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

Buttons can be added via -addButtonWithTitle::

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

The return code tells you which button was pressed:

- (void) alertDidEnd:(NSAlert *)a returnCode:(NSInteger)rc contextInfo:(void *)ci {
    switch(rc) {
        case NSAlertFirstButtonReturn:
            // "First" pressed
            break;
        case NSAlertSecondButtonReturn:
            // "Second" pressed
            break;
        // ...
    }
}
like image 106
Georg Fritzsche Avatar answered Nov 11 '22 15:11

Georg Fritzsche


Long time has passed since the accepted answer and things have changed:

  • Swift is becoming more and more popular.
  • beginSheetModalForWindow(_:modalDelegate:didEndSelector:contextInfo:) is deprecated, we should use beginSheetModalForWindow:completionHandler: instead.

Latest code sample in 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
    }
}

In case you want a synchronous version:

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
    }
}
like image 28
Tyler Liu Avatar answered Nov 11 '22 15:11

Tyler Liu