使用Swift创建NSAlert

Tom*_*mer 40 cocoa nsalert swift

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

警报是确认用户想要删除文档.

我希望"删除"按钮然后运行删除功能和"取消"按钮只是为了解除警报.

我怎么能在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];
Run Code Online (Sandbox Code Playgroud)

aya*_*aio 125

beginSheetModalForWindow:modalDelegate 在OS X 10.10 Yosemite中弃用.

斯威夫特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.")
Run Code Online (Sandbox Code Playgroud)

这将返回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.")
Run Code Online (Sandbox Code Playgroud)

斯威夫特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.")
Run Code Online (Sandbox Code Playgroud)

  • 确保`import AppKit`(至少在swift 3中) (3认同)
  • @Claude如果你正在发出警报,这意味着你正在制作一个Cocoa应用程序,这意味着你正在导入已经导入AppKit的Cocoa. (2认同)

小智 23

我想这可能对你有用......

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")
    }
})
Run Code Online (Sandbox Code Playgroud)


Jam*_*rch 8

更新了Jose Hidalgo对Swift 4的回答:

let a: NSAlert = NSAlert()
a.messageText = "Delete the document?"
a.informativeText = "Are you sure you would like to delete the document?"
a.addButton(withTitle: "Delete")
a.addButton(withTitle: "Cancel")
a.alertStyle = NSAlert.Style.warning

a.beginSheetModal(for: self.window!, completionHandler: { (modalResponse: NSApplication.ModalResponse) -> Void in
    if(modalResponse == NSApplication.ModalResponse.alertFirstButtonReturn){
        print("Document deleted")
    }
})
Run Code Online (Sandbox Code Playgroud)


den*_*T30 5

选择@Jose Hidalgo 对Swift 5的回答

        let a = NSAlert()
        a.messageText = "Delete the document?"
        a.informativeText = "Are you sure you would like to delete the document?"
        //   .alertFirstButtonReturn
        a.addButton(withTitle: "Delete")

        //   .alertSecondButtonReturn
        a.addButton(withTitle: "Cancel")
        a.alertStyle = .warning
        var w: NSWindow?
        if let window = view.window{
            w = window
        }
        else if let window = NSApplication.shared.windows.first{
            w = window
        }
        if let window = w{
            a.beginSheetModal(for: window){ (modalResponse) in
                if modalResponse == .alertFirstButtonReturn {
                    print("Document deleted")
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)