<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSAlert",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAlert"
  },
  "title" : "NSAlert"
}
-->

# NSAlert

A modal dialog or sheet attached to a document window.

```
@MainActor class NSAlert
```

## Overview

The methods of the [`NSAlert`](/documentation/AppKit/NSAlert) class allow you to specify alert level, alert text, button titles, and a custom icon should you require it. The class also lets your alerts display a help button and provides ways for apps to offer help specific to an alert.

To display an alert as a sheet, call the [`beginSheetModal(for:completionHandler:)`](/documentation/AppKit/NSAlert/beginSheetModal(for:completionHandler:)) method; to display one as an app-modal dialog, use the [`runModal()`](/documentation/AppKit/NSAlert/runModal()) method.

By design, an [`NSAlert`](/documentation/AppKit/NSAlert) object is intended for a single alert—that is, an alert with a unique combination of title, buttons, and so on—that is displayed upon a particular condition. You should create an [`NSAlert`](/documentation/AppKit/NSAlert) object for each alert dialog, creating it only  when you need to display an alert, and release it when you are done. If you have a particular alert dialog that you need to show repeatedly, you can retain and reuse an instance of [`NSAlert`](/documentation/AppKit/NSAlert) for this dialog.

After creating an alert using one of the alert creation methods, you can customize it further prior to displaying it by customizing its attributes. See [`Instance Attributes`](/documentation/AppKit/NSAlert#Instance-Attributes).

Unless you must maintain compatibility with existing alert-processing code that uses the function-based API, you should allocate (`alloc`) and initialize (`init`) the alert object, and then set its attributes using the appropriate methods of the `NSAlert` class.

### Instance Attributes

[`NSAlert`](/documentation/AppKit/NSAlert) objects have the following attributes:

- Type An alert’s type helps convey the importance or gravity of its message to the user. Specified with the [`alertStyle`](/documentation/AppKit/NSAlert/alertStyle) property.
- Message text The main message of the alert. Specified with [`messageText`](/documentation/AppKit/NSAlert/messageText).
- Informative text Additional information about the alert. Specified with [`informativeText`](/documentation/AppKit/NSAlert/informativeText).
- Icon An optional, custom icon to display in the alert, which is used instead of the default app icon. Specified with [`icon`](/documentation/AppKit/NSAlert/icon).
- Help Alerts can let the user get help about them. Use [`helpAnchor`](/documentation/AppKit/NSAlert/helpAnchor) and [`showsHelp`](/documentation/AppKit/NSAlert/showsHelp).
- Response buttons By default an alert has one response button: the OK button. You can add more response buttons using the [`addButton(withTitle:)`](/documentation/AppKit/NSAlert/addButton(withTitle:)) method.
- Suppression checkbox A suppression checkbox allows the user to suppress the display of a particular alert in subsequent occurrences of the event that triggers it. Use [`showsSuppressionButton`](/documentation/AppKit/NSAlert/showsSuppressionButton).
- Accessory view An accessory view lets you add additional information to an alert; for example, a text field with contact information. Use [`accessoryView`](/documentation/AppKit/NSAlert/accessoryView), [`layout()`](/documentation/AppKit/NSAlert/layout()).

### Subclassing Notes

The `NSAlert` class is not designed for subclassing.

## Topics

### Creating Alerts

[`init(error:)`](/documentation/AppKit/NSAlert/init(error:))

Returns an alert initialized from information in an error object.

[`alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:`](/documentation/AppKit/NSAlert/alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:)

Creates an alert compatible with alerts created using the [`NSRunAlertPanel`](/documentation/AppKit/NSRunAlertPanel) function for display as a warning-style alert.

### Configuring Alerts

[`layout()`](/documentation/AppKit/NSAlert/layout())

Specifies that the alert must do immediate layout instead of lazily just before display.

[`alertStyle`](/documentation/AppKit/NSAlert/alertStyle)

Indicates the alert’s severity level.

[`accessoryView`](/documentation/AppKit/NSAlert/accessoryView)

The alert’s accessory view.

[`showsHelp`](/documentation/AppKit/NSAlert/showsHelp)

Specifies whether the alert has a help button.

[`helpAnchor`](/documentation/AppKit/NSAlert/helpAnchor)

The alert’s HTML help anchor.

[`delegate`](/documentation/AppKit/NSAlert/delegate)

The alert’s delegate.

### Displaying Alerts

[`runModal()`](/documentation/AppKit/NSAlert/runModal())

Runs the alert as an app-modal dialog and returns the constant that identifies the button clicked.

[`beginSheetModal(for:completionHandler:)`](/documentation/AppKit/NSAlert/beginSheetModal(for:completionHandler:))

Runs the alert modally as a sheet attached to the specified window.

[`beginSheetModal(for:modalDelegate:didEnd:contextInfo:)`](/documentation/AppKit/NSAlert/beginSheetModal(for:modalDelegate:didEnd:contextInfo:))

Runs the alert modally as an alert sheet attached to a specified window.

[`suppressionButton`](/documentation/AppKit/NSAlert/suppressionButton)

The alert’s suppression checkbox.

[`showsSuppressionButton`](/documentation/AppKit/NSAlert/showsSuppressionButton)

Specifies whether the alert includes a suppression checkbox, which you can employ to allow a user to opt out of seeing the alert again.

### Accessing Alert Text

[`informativeText`](/documentation/AppKit/NSAlert/informativeText)

The alert’s informative text.

[`messageText`](/documentation/AppKit/NSAlert/messageText)

The alert’s message text or title.

### Accessing a Custom Alert Icon

[`icon`](/documentation/AppKit/NSAlert/icon)

The custom icon displayed in the alert.

### Accessing Alert Response Buttons

[`buttons`](/documentation/AppKit/NSAlert/buttons)

The array of response buttons for the alert.

[`addButton(withTitle:)`](/documentation/AppKit/NSAlert/addButton(withTitle:))

Adds a button with a given title to the alert.

### Getting Alert Windows

[`window`](/documentation/AppKit/NSAlert/window)

The app-modal panel or document-modal sheet that corresponds to the alert.

### Constants

[`NSAlert.Style`](/documentation/AppKit/NSAlert/Style)

The set of alert styles to style alerts in your app.

[`NSApplication.ModalResponse`](/documentation/AppKit/NSApplication/ModalResponse)

A set of button return values for modal dialogs.

## See Also

  [Dialogs and Special Panels](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Dialog/Dialog.html#//apple_ref/doc/uid/10000071i)



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)