2017-04-27 156 views
1

我试图做简单的警报在新的Xcode 8.3.2更新我正在同时提出警告对话框中面临的问题:无法将'UIAlertAction'类型的值转换为期望的参数类型'UIViewController'?

@IBAction func testButonAlert() 
{ 

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

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

错误:

不能键入“UIAlertAction”的值转换为预期参数类型 '的UIViewController'

enter image description here

回答

2

您将出示action这是不可能的(并导致错误)。

你需要家长UIAlertController的动作重视,例如:

@IBAction func testButonAlert() 
{ 
    let alertController = UIAlertController(title: "Hi TEst", message: "Choose the action", preferredStyle: .alert) 
    let alertAction = UIAlertAction(title : "Delete Me" , 
            style : .destructive) { action in 
             print("action triggered") 
            } 

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

您可以简单地创建警报与UIAlertController

let yourAlert = UIAlertController(title: "Alert header", message: "Alert message text.", preferredStyle: UIAlertControllerStyle.alert) 
yourAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (handler) in 
         //You can also add codes while pressed this button 
        })) 
self.present(yourAlert, animated: true, completion: nil) 
3

您应该使用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) 
0

对于雨燕4.0

class func showAlert(_ title: String, message: String, viewController: UIViewController, 
         okHandler: @escaping ((_: UIAlertAction) -> Void), 
         cancelHandler: @escaping ((_: UIAlertAction) -> Void)) { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let OKAction = UIAlertAction(title: "OK", style: .default, handler: okHandler) 
     alertController.addAction(OKAction) 
     let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: cancelHandler) 
     alertController.addAction(cancelAction) 
     viewController.present(alertController, animated: true, completion: nil) 
    } 
相关问题