2015-04-03 74 views
23

我有Objective-C中创建和NSAlert的代码,但我现在想在Swift中创建它。用Swift创建一个NSAlert

该警报旨在确认用户想要删除文档。

我想“删除”按钮然后运行删除功能和“取消”一个只是为了解除警报。

如何在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]; 
+0

你可能要考虑的是'beginSheetModal(用于:completionHandler:)'是*不*过时,实际上它可能是更理想的方式来处理你的模态对话框(在一个块中)。它也会更接近老式的'didEndSelector',它不会停止整个应用程序。 – Patru 2017-07-01 13:30:38

回答

78

beginSheetModalForWindow:modalDelegate在OS X 10.10优胜美地已被弃用。

夫特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.") 

这将返回根据用户的选择或truefalse

NSAlertFirstButtonReturn代表添加到对话框的第一个按钮,这里是“OK”之一。

斯威夫特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.") 

斯威夫特4

我们现在使用的警报的风格按钮选择枚举。

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.") 
+1

请确保'import AppKit'(至少在swift 3中) – Claude 2017-02-03 11:50:04

+1

@Claude如果你正在发出警报,这意味着你正在制作一个Cocoa应用程序,这意味着你正在导入已经导入AppKit的Cocoa。 – Moritz 2017-02-03 12:09:00

+0

可能我不应该从非VC类中发出警报;但我只是想要一些穷人的错误显示。这个util类没有导入除了Foundation之外的任何东西,所以我需要导入(至少它让XCode很开心)。 – Claude 2017-02-03 12:22:52

13

我想这可能为你工作...

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 = NSAlertStyle.WarningAlertStyle 

    a.beginSheetModalForWindow(self.view.window!, completionHandler: { (modalResponse) -> Void in 
     if modalResponse == NSAlertFirstButtonReturn { 
      print("Document deleted") 
     } 
    })