Contents

NSAlert

A modal dialog or sheet attached to a document window.

Declaration

@MainActor class NSAlert

Overview

The methods of the NSAlert class let you specify an alert’s urgency, message text, button titles, and a custom icon. The class also supports a help button and other ways to offer contextual help specific to an alert.

To display an alert as a sheet, call the beginSheetModal(for:completionHandler:) method; to display one as an app-modal dialog, use the runModal() method.

By design, each NSAlert object represents a single alert with a specific combination of title, buttons, and other attributes that appear in response to a particular condition. Allocate (alloc) and initialize (init) an NSAlert object for each alert dialog, customize its attributes as described below, and release it once you’re done. If you need to show a particular alert repeatedly, retain and reuse a single instance instead of creating a new one each time.

NSAlert objects have the following attributes:

Type

The importance or urgency of the alert, as shown to the person viewing it. Specified with the alertStyle property.

Message text

The main message of the alert. Specified with messageText.

Informative text

Additional information about the alert. Specified with informativeText.

Icon

An optional, custom icon to display in the alert, used instead of the default app icon. Specified with icon.

Help

A help button that a person can click to get more information about the alert. Use helpAnchor and showsHelp.

Response buttons

By default an alert has one response button: the OK button. You can add more response buttons using the addButton(withTitle:) method.

Suppression checkbox

A checkbox that lets a person opt out of seeing this particular alert again. Use showsSuppressionButton.

Accessory view

A custom view, such as a text field for entering contact information, that adds extra content to an alert. Use accessoryView and layout().

Topics

Creating alerts

Configuring alerts

Displaying alerts

Accessing alert text

Accessing a custom alert icon

Accessing alert response buttons

Getting alert windows

Deprecated

See Also

Related Documentation

Alerts