如何在可可中显示警告弹出窗口?

时间:2013-08-24 10:16:08

标签: cocoa alert

我想显示一个显示信息行的弹出窗口。他们的任何东西都在ios中的可可UIAlertView中,以及如何弹出它们。 感谢

6 个答案:

答案 0 :(得分:38)

您可以在cocoa中使用NSAlert。这与ios中的UIAlertView相同。 您可以通过此

弹出警报
NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"Ok" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"Alert pop up displayed"];
[alert runModal];

修改

这是最新使用的方法,因为现在不推荐使用上述方法。

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Message text."];
[alert setInformativeText:@"Informative text."];
[alert addButtonWithTitle:@"Cancel"];
[alert addButtonWithTitle:@"Ok"];
[alert runModal];

答案 1 :(得分:13)

Swift 3.0

let alert = NSAlert.init()
alert.messageText = "Hello world"
alert.informativeText = "Information text"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
alert.runModal()

答案 2 :(得分:5)

有一个狡猾的NSAlert类,它可以显示一个对话框或工作表来显示你的警报。

答案 3 :(得分:2)

你可以在Swift中使用这个方法

 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.")
选择" OK"

回答是真是假。或"取消"分别

答案 4 :(得分:2)

Swift 5.1

func confirmAbletonIsReady(question: String, text: String) -> Bool {
    let alert = NSAlert()
    alert.messageText = question
    alert.informativeText = text
    alert.alertStyle = NSAlert.Style.warning
    alert.addButton(withTitle: "OK")
    alert.addButton(withTitle: "Cancel")
    return alert.runModal() == NSApplication.ModalResponse.alertFirstButtonReturn
}

@Giang的更新

答案 5 :(得分:1)

Swift 3.0示例:

声明:

 func showCloseAlert(completion : (Bool)->Void) {
        let alert = NSAlert()
        alert.messageText = "Warning!"
        alert.informativeText = "Nothing will be saved!"
        alert.alertStyle = NSAlertStyle.warning
        alert.addButton(withTitle: "OK")
        alert.addButton(withTitle: "Cancel")
        completion(alert.runModal() == NSAlertFirstButtonReturn)
 }

用法:

    showCloseAlert { answer in
        if answer == true{
            self.dismissViewController(self)
        }
    }