Creating a Simple Alert Dialog

If you’re creating a macOS program, you may want to display a dialog box on the screen to alert the user of something, such as an error message of some kind. The simplest way to create a dialog is to use the NSAlert class like this:

        let myAlert = NSAlert()
        myAlert.messageText = "Message text"
        myAlert.informativeText = "Informative text"
        myAlert.runModal()

If you create a push button on a window and store the above code in an IBAction method for This code creates a simple dialog that looks like this:

The messageText property defines the bold text and the informativeText property defines the text that appears underneath.

As an alternative to displaying a modal dialog, you can also display a sheet instead by using this code:

        let myAlert = NSAlert()
        myAlert.messageText = "Message text"
        myAlert.informativeText = "Informative text"
        myAlert.beginSheetModal(for: self.view.window!, completionHandler: nil)

This creates a sheet like this:

A simple dialog that displays a message is fine. When the user wants to dismiss the dialog, he or she can just click the OK button. However in many cases, you may want to add up to three buttons on a dialog with custom text. To do that, you need to use the addButton method like this:

        myAlert.addButton(withTitle: "Save")
        myAlert.addButton(withTitle: "Delete")
        myAlert.addButton(withTitle: "Cancel")

To determine which button the user clicked, you need to use a switch statement. If you created a modal dialog using the runModal() method, your switch statement might look like this:

        switch myAlert.runModal() {
            case NSAlertFirstButtonReturn: print ("Save")
            case NSAlertSecondButtonReturn: print ("Delete")
            case NSAlertThirdButtonReturn: print ("Cancel")
            default: print ("Nothing")
        }

So if you placed a push button on a window, and created an IBAction method to display a dialog by clicking this push button, your entire code might look like this:

    @IBAction func showAlert(_ sender: NSButton) {
        let myAlert = NSAlert()
        myAlert.messageText = "Message text"
        myAlert.informativeText = "Informative text"
        
        myAlert.addButton(withTitle: "Save")
        myAlert.addButton(withTitle: "Delete")
        myAlert.addButton(withTitle: "Cancel")
        
        switch myAlert.runModal() {
            case NSAlertFirstButtonReturn: print ("Save")
            case NSAlertSecondButtonReturn: print ("Delete")
            case NSAlertThirdButtonReturn: print ("Cancel")
            default: print ("Nothing")
        }
    }

To determine which button the user clicked if you created a sheet, your switch statement would look slightly differently:

        myAlert.beginSheetModal(for: self.view.window!, completionHandler: { (returnCode) -> Void in
            switch returnCode {
                case NSAlertFirstButtonReturn: print ("Save")
                case NSAlertSecondButtonReturn: print ("Delete")
                case NSAlertThirdButtonReturn: print ("Cancel")
                default: print ("Nothing")
            }
        })

If you created a push button on a window with an IBAction method to open a sheet, your entire IBAction method code might look like this:

@IBAction func showAlert(_ sender: NSButton) {
        let myAlert = NSAlert()
        myAlert.messageText = "Message text"
        myAlert.informativeText = "Informative text"
        
        myAlert.addButton(withTitle: "Save")
        myAlert.addButton(withTitle: "Delete")
        myAlert.addButton(withTitle: "Cancel")

        myAlert.beginSheetModal(for: self.view.window!, completionHandler: { (returnCode) -> Void in
            switch returnCode {
                case NSAlertFirstButtonReturn: print ("Save")
                case NSAlertSecondButtonReturn: print ("Delete")
                case NSAlertThirdButtonReturn: print ("Cancel")
                default: print ("Nothing")
            }
        })

    }

By using the NSAlert class, you an easily create and display dialogs as modal dialog boxes or as a sheet. You an also customize the number of buttons (up to three) and retrieve which button the user clicked on.

April 16th, 2017 by
HTML Snippets Powered By : XYZScripts.com