“类型'UIAlertController'没有成员'样式'”

时间:2019-05-19 06:57:23

标签: uikit

尝试创建警报框,但是出现一个问题,提示“类型'UIAlertController'没有成员'样式'”

@IBAction func showMessage (sender: UIButton) {
       let alertController = UIAlertController(title: "Welcome to My First App",
message: "Hello World", preferredStyle: UIAlertController.Style.alert);
       alertController.addAction(UIAlertAction(title: "OK", style:
    UIAlertAction.Style.default, handler:nil))
       present(alertController, animated:true, completion: nil)

1 个答案:

答案 0 :(得分:0)

UIAlertController具有以下参数:

convenience init(title: String?, 
         message: String?, 
  preferredStyle: UIAlertController.Style)

UIAlertController.Style的常量是actionSheet和alert。 因此,您可以执行以下操作(同样适用于UIAlertAction):

let alertController = UIAlertController(
            title: "Welcome to my First App",
            message: "Hello World",
            preferredStyle: .alert)

alertController.addAction(UIAlertAction(
            title: "OK",
            style: .default,
            handler: nil))