我创建了一个名为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
}