2015-04-03 75 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") 
     } 
    })