swift - 无法将类型 'UIAlertAction' 的值转换为预期的参数类型 'UIViewController'?

标签 swift uialertcontroller

我正在尝试在新的 Xcode 更新 8.3.2 中做简单的警报我在显示警报对话框时遇到问题:

@IBAction func testButonAlert()
{

    let alertAction = UIAlertAction( title   : "Hi TEst" ,
                                     style   : UIAlertActionStyle.destructive,
                                     handler : { ( UIAlertActionStyle) -> Void in print("") })

    self.present(alertAction , animated: false, completion: nil)
}

错误:

Cannot convert value of type 'UIAlertAction' to expected argument type 'UIViewController'

enter image description here

最佳答案

你应该使用 UIAlertController。

let alertVC = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
self.present(alertVC, animated: true, completion: nil)

关于swift - 无法将类型 'UIAlertAction' 的值转换为预期的参数类型 'UIViewController'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43656480/

相关文章:

ios - 当应用程序在后台时快速显示警报

ios - 使 UIAlertControlleraccessibilityLabel 与标题不同

swift - 在 Swift 中迭代和修改结构集合

swift - 使 XCTAssertions 接受函数返回值作为参数

ios - 在 Swift 中连接字符串

ios - 如何将图像放在 UIActionSheet 上?

ios - 如何在 swift 4 中实现 make 处理程序以使代码能够按需要的顺序执行

swift - 在 tableview 中显示文档目录中的音频文件并在 swift 3 中选择行时播放音频文件

ios - 在 IOS 中使用 Webview 时 Admob 不显示广告

ios - Swift:通过抛出转义闭包进行错误处理(令人困惑)