如何在MacOS的父视图中显示警报

时间:2020-10-08 21:25:25

标签: swift xcode macos sprite-kit

我创建了一个名为Alertable的协议,该协议用于 我的班级GameScene:SKScene,机警{ 我可以在iOS上使用,但不能在MacOS上使用

extension Alertable where Self: SKScene {

    func showAlert(withTitle title: String, message: String) {
        #if os(iOS) || os(tvOS)
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

            let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
            alertController.addAction(okAction)

            view?.window?.rootViewController?.present(alertController, animated: true)
        #else // MacOS version
            let alert = NSAlert()
            alert.messageText = title
            alert.informativeText = message
            alert.addButton(withTitle: "OK")
            alert.addButton(withTitle: "Cancel")
            //alert.runModal() == .alertFirstButtonReturn
            //view?.window?.contentViewController?.present(alert)
        #endif
    }

0 个答案:

没有答案