如何在 Swift Playgrounds 中获得弹出对话框
How do I get a popup dialog box in Swift Playgrounds
我想知道如何在 Swift Playgrounds 中弹出对话框(是的,必须在 Playgrounds 中)我已经尝试了以下代码(直接来自AppleDevs 网站)
但是,无论我尝试什么,self
标签 总是 会引发错误。谁能帮我解决这个问题?
import UIKit
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
需要从视图控制器中显示警报。 than 意味着它将出现在助理编辑器内的模拟器中:
示例:
import UIKit
import PlaygroundSupport
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
let v = UIViewController()
PlaygroundPage.current.liveView = v
v.present(alert, animated: true, completion: nil)
我想知道如何在 Swift Playgrounds 中弹出对话框(是的,必须在 Playgrounds 中)我已经尝试了以下代码(直接来自AppleDevs 网站)
但是,无论我尝试什么,self
标签 总是 会引发错误。谁能帮我解决这个问题?
import UIKit
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
self.present(alert, animated: true, completion: nil)
需要从视图控制器中显示警报。 than 意味着它将出现在助理编辑器内的模拟器中:
示例:
import UIKit
import PlaygroundSupport
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in
NSLog("The \"OK\" alert occured.")
}))
let v = UIViewController()
PlaygroundPage.current.liveView = v
v.present(alert, animated: true, completion: nil)