Last active
January 20, 2021 04:08
-
-
Save marteinn/540cfa4282add987ee6b to your computer and use it in GitHub Desktop.
NSAlert Example: How to show a NSAlert as a sheet in a NSView (Cocoa)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSAlert *alert = [[NSAlert alloc] init]; | |
[alert setMessageText:@"Delete this project?"]; | |
[alert setInformativeText:@"Deleted projects cannot be restored"]; | |
[alert addButtonWithTitle:@"OK"]; | |
[alert addButtonWithTitle:@"Cancel"]; | |
[alert setAlertStyle:NSWarningAlertStyle]; | |
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) { | |
if (returnCode == NSAlertSecondButtonReturn) { | |
NSLog(@"Delete was cancelled!"); | |
return; | |
} | |
NSLog(@"This project was deleted!"); | |
}]; |
Hi @Klaus-Valse! In my case I'm opening a alert through a viewController, that controller has the window
property.
Sorry for the late response, next time tag me so I get a notification :)
@marteinn is this for Swift 3? It looks like it's for Swift 2
@sn0wyfall Nope, this is Objective-C.
It appears that NSWarningAlertStyle has been deprecated
NSAlertStyle enum now has NSAlertStyleCritical, NSAlertStyleInformational, and NSAlertStyleWarning
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't understand line 8 to 15. Can you please explain? I do not have a property that is called "window". What do I do? All I have is a single window application.
Kind regards