<!--
{
  "availability" : [
    "macCatalyst: -",
    "macOS: 10.0.0 - 10.10.0"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSBeginAlertSheet",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:@F@NSBeginAlertSheet"
  },
  "title" : "NSBeginAlertSheet"
}
-->

# NSBeginAlertSheet

Creates and runs an alert sheet.

```
extern void NSBeginAlertSheet(NSString *title, NSString *defaultButton, NSString *alternateButton, NSString *otherButton, NSWindow *docWindow, id modalDelegate, SEL didEndSelector, SEL didDismissSelector, void *contextInfo, NSString *msgFormat, ...);
```

## Discussion

Creates and runs an alert sheet on `docWindow`, with the title of `title`, the text of `msg`, and buttons with titles of `defaultButton`, `alternateButton`, and `otherButton`.

The buttons are laid out on the lower-right corner of the sheet, with `defaultButton` on the right, `alternateButton` on the left, and `otherButton` in the middle. If `title` is `nil` or an empty string, a default localized title is used (“Alert” in English). If `defaultButton` is `nil` or an empty string, a default localized button title (“OK” in English) is used. For the remaining buttons, this function creates them only if their corresponding button title is non-`nil`.

A Command-D key equivalent for the “Don’t Save” button is provided, if one is found. The button titles are searched for the localized value for “Don’t Save.” If a match is found, that button is assigned a Command-D key equivalent, provided it is not the default button.

If you create a modal panel using [`runModal(for:)`](/documentation/AppKit/NSApplication/runModal(for:)) or [`beginSheet(_:modalFor:modalDelegate:didEnd:contextInfo:)`](/documentation/AppKit/NSApplication/beginSheet(_:modalFor:modalDelegate:didEnd:contextInfo:)), you can assign the key equivalent yourself, using [`keyEquivalent`](/documentation/AppKit/NSButton/keyEquivalent) and [`keyEquivalentModifierMask`](/documentation/AppKit/NSButton/keyEquivalentModifierMask).

The `msg` argument is the message that’s displayed in the panel. It can use printf-style formatting characters; any necessary arguments should be listed at the end of the function’s argument list (after the `msg` argument). For more information on formatting characters, see the man page for `printf`.

When the modal session is ended, and before the sheet is dismissed, the `didEndSelector` is invoked on the `modalDelegate`. passing `contextInfo`. After the sheet is dismissed, the `didDismissSelector` is invoked on the `modalDelegate`, passing `contextInfo`. Typically, you will want to implement the `didEndSelector` but you may pass `NULL` for the `didDismissSelector`. The two selectors should be defined as follows:

```objc
sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void  *)contextInfo;
sheetDidDismiss:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void  *)contextInfo;
```

where `sheet` is the alert sheet, `returnCode` specifies which button the user pressed, and `contextInfo` is the same `contextInfo` passed into [`NSBeginAlertSheet`](/documentation/AppKit/NSBeginAlertSheet). `returnCode` can be one of the following:

- [`NSAlertDefaultReturn`](/documentation/AppKit/NSAlertDefaultReturn) means the user pressed the default button.
- [`NSAlertAlternateReturn`](/documentation/AppKit/NSAlertAlternateReturn) means the user pressed the alternate button.
- [`NSAlertOtherReturn`](/documentation/AppKit/NSAlertOtherReturn) means the user pressed the other button.
- [`NSAlertErrorReturn`](/documentation/AppKit/NSAlertErrorReturn) means an error occurred while running the alert panel.

---

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)