使用Swift创建NSAlert

mfpqipee  于 2023-02-21  发布在  Swift
关注(0)|答案(6)|浏览(135)

我有代码来创建和NSAlert在Objective-C,但我现在想创建它在斯威夫特。
该警报用于确认用户想要删除文档。
我想“删除”按钮,然后运行删除功能和“取消”一个只是为了解除警报。
我怎么用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];
r1wp621o

r1wp621o1#

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.")
lymnna71

lymnna712#

我觉得这对你有用...

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")
    }
})
3pmvbmvn

3pmvbmvn3#

更新了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")
    }
})
f8rj6qna

f8rj6qna4#

opt @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")
                }
            }
        }
cbwuti44

cbwuti445#

我尝试了上述解决方案,但我不能得到适当大小的警报。我已经定义了大小的警报了。

let alert = NSAlert()
        alert.messageText = "YOUR MESSAGE"
        alert.addButton(withTitle: "BUTTON1")
        alert.addButton(withTitle: "BUTTON2")
        var frame = alert.window.frame
        frame.size.height = 300
        frame.size.width = 200
        alert.window.setFrame(frame, display: true)
        
        let stackViewer = NSStackView(frame: NSRect(x: 0, y: 0, width: 200, height: 00))
        alert.accessoryView = stackViewer
        
        
        alert.beginSheetModal(for: self.view.window!, completionHandler: { (modalResponse) -> Void in
                if modalResponse == NSApplication.ModalResponse.alertFirstButtonReturn {
                    print("first btn")
                    
                }else{
                    print("second btn")
              
            }
        })
dkqlctbz

dkqlctbz6#

let anAlert = NSAlert()
    anAlert.messageText = "Exit?"
    anAlert.informativeText = "Changes will not be saved!"
    anAlert.alertStyle = .warning
    anAlert.addButton(withTitle: "Yes")
    anAlert.addButton(withTitle: "No")
    if anAlert.runModal() == .alertFirstButtonReturn {
        return .terminateNow
    }
    return .terminateLater

相关问题