Noob
Noob

Reputation: 2927

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

i am trying to do simple alert in new Xcode update 8.3.2 i am facing problem while presenting the alert dialog :

@IBAction func testButonAlert()
{

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

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

Error :

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

enter image description here

Upvotes: 3

Views: 8089

Answers (4)

Ramanpreet Singh
Ramanpreet Singh

Reputation: 91

For Swift 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)
    }

Upvotes: 0

vadian
vadian

Reputation: 285290

You are going to present the action which is impossible (and causes the error).

You need a parent UIAlertController to attach the action to for example:

@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)
}

Upvotes: 2

kkakkurt
kkakkurt

Reputation: 2800

You can simply create alerts with 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)

Upvotes: 3

Halil İbrahim YILMAZ
Halil İbrahim YILMAZ

Reputation: 750

You should use 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)

Upvotes: 3

Related Questions