Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an NSAlert with Swift

I have the code to create and NSAlert in Objective-C but I would now like to create it in Swift.

The alert is to confirm that the user would like to delete a document.

I would like the "delete" button to then run the delete function and the "cancel" one just to dismiss the alert.

How can I write this in Swift?

NSAlert *alert = [[[NSAlert alloc] init] autorelease]; [alert addButtonWithTitle:@"Delete"]; [alert addButtonWithTitle:@"Cancel"]; [alert setMessageText:@"Delete the document?"]; [alert setInformativeText:@"Are you sure you would like to delete the document?"]; [alert setAlertStyle:NSWarningAlertStyle]; [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil]; 
like image 681
Tom Coomer Avatar asked Apr 03 '15 13:04

Tom Coomer


2 Answers

beginSheetModalForWindow:modalDelegate is deprecated in OS X 10.10 Yosemite.

Swift 2

func dialogOKCancel(question: String, text: String) -> Bool {     let alert: NSAlert = NSAlert()     alert.messageText = question     alert.informativeText = text     alert.alertStyle = NSAlertStyle.WarningAlertStyle     alert.addButtonWithTitle("OK")     alert.addButtonWithTitle("Cancel")     let res = alert.runModal()     if res == NSAlertFirstButtonReturn {         return true     }     return false }  let answer = dialogOKCancel("Ok?", text: "Choose your answer.") 

This returns true or false according to the user's choice.

NSAlertFirstButtonReturn represents the first button added to the dialog, here the "OK" one.

Swift 3

func dialogOKCancel(question: String, text: String) -> Bool {     let alert = NSAlert()     alert.messageText = question     alert.informativeText = text     alert.alertStyle = NSAlertStyle.warning     alert.addButton(withTitle: "OK")     alert.addButton(withTitle: "Cancel")     return alert.runModal() == NSAlertFirstButtonReturn }  let answer = dialogOKCancel(question: "Ok?", text: "Choose your answer.") 

Swift 4

We now use enums for the alert's style and the button selection.

func dialogOKCancel(question: String, text: String) -> Bool {     let alert = NSAlert()     alert.messageText = question     alert.informativeText = text     alert.alertStyle = .warning     alert.addButton(withTitle: "OK")     alert.addButton(withTitle: "Cancel")     return alert.runModal() == .alertFirstButtonReturn }  let answer = dialogOKCancel(question: "Ok?", text: "Choose your answer.") 
like image 89
Eric Aya Avatar answered Oct 02 '22 12:10

Eric Aya


I think this may work for you...

let a = NSAlert() a.messageText = "Delete the document?" a.informativeText = "Are you sure you would like to delete the document?" a.addButtonWithTitle("Delete") a.addButtonWithTitle("Cancel") a.alertStyle = NSAlert.Style.WarningAlertStyle  a.beginSheetModalForWindow(self.view.window!, completionHandler: { (modalResponse) -> Void in     if modalResponse == NSAlertFirstButtonReturn {         print("Document deleted")     } }) 
like image 23
Jose Hidalgo Avatar answered Oct 02 '22 13:10

Jose Hidalgo