2017-04-27 134 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) 
    } 
相關問題