如果用户没有单击任何按钮,如何在一段时间后关闭 NSAlert

how to close NSAlert after some time if no button is clicked by user

如果用户没有点击任何按钮,我想在特定时间后关闭警报 Window (NSAlert)。有没有办法设置 NSTimer 在一段时间后关闭警报面板,比如 30 秒。

我刚刚做了类似的事情。我在这里为您修改了一些代码:

class ViewController: NSViewController {

    var progressWindow:NSAlert!

    func testOfProgressWindow() {
        progressWindow = NSAlert()
        progressWindow.alertStyle = .InformationalAlertStyle
        progressWindow.messageText = "some notes"

        let date = NSDate(timeInterval: 30, sinceDate: NSDate())
        let uiUpdateTimer = NSTimer(fireDate: date, interval: 0, target: self, selector: "closeSheet", userInfo: nil, repeats: false)
        let runLoop = NSRunLoop.mainRunLoop()
        runLoop.addTimer(uiUpdateTimer, forMode: NSDefaultRunLoopMode)

        progressWindow.beginSheetModalForWindow(self.view.window!, completionHandler: nil)
    }

    func closeSheet() {
        self.view.window?.endSheet(self.progressWindow.window)
    }

}