【问题标题】:How do I get a popup dialog box in Swift Playgrounds如何在 Swift Playgrounds 中获得弹出对话框
【发布时间】:2019-10-25 00:22:11
【问题描述】:

我想知道如何在 Swift Playgrounds 中弹出一个对话框(是的,必须在 Playgrounds 中)我尝试了以下代码(直接来自 AppleDevs 网站)

但是,无论我尝试什么,self 标签 always 都会引发错误。谁能帮我解决这个问题?

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)

【问题讨论】:

  • 创建 Playground 文件时选择单一视图
  • 在哪里? “单一视图”根本没有列出

标签: swift swift-playground


【解决方案1】:

需要从视图控制器呈现警报。比意味着它会出现在助理编辑器内的模拟器中:

例子:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    • 2020-06-29
    • 2020-06-26
    相关资源
    最近更新 更多