如何在Swift Playgrounds中获取一个弹出式对话框

3
最初的回答:我想知道如何在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)

1
创建您的 playground 文件时,请选择单个视图。 - Leo Dabus
1
在哪里?“Single View”根本没有被列出来。 - VoxelPrismatic
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
6

提示需要从视图控制器中呈现。这意味着它会在助理编辑器内的模拟器中显示:

示例:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,