Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type "ViewController.Type" to expected argument type "UIViewController"

Tags:

ios

swift

I'm trying to make an Alert Controller in which, if the Answer is "Ok", then it will perform a segue to a MapView. Here's the full code:

@IBAction func teste(_ sender: Any) {

    // Create the alert controller
    let alertController = UIAlertController(title: "Reservar vaga?", message: "Uma vaga será reservada em Estapar Estacionamento.", preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {(alert:UIAlertAction) -> Void in

        let confirmAction = UIAlertController(title: "Vaga confirmada", message: "Gostaria de ter direções ao local?", preferredStyle: .alert)

        let okConfirmAction = UIAlertAction(title:"Sim", style: UIAlertActionStyle.default, handler:{(alert:UIAlertAction) -> Void in

            presentViewController(ViewController, animated: true, completion: nil)
        })

        let noConfirmAction = UIAlertAction(title:"Não", style: UIAlertActionStyle.default) {
            UIAlertAction in
            NSLog("Ok Pressed")
        }

        confirmAction.addAction(okConfirmAction)
        confirmAction.addAction(noConfirmAction)
        self.present(confirmAction, animated: true, completion: nil)
    })
    let cancelAction = UIAlertAction(title: "Cancelar", style: UIAlertActionStyle.cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    //Append the button to the view
    self.present(alertController, animated: true, completion: nil)
}

I'm having trouble in this part:

let okConfirmAction = UIAlertAction(title:"Sim", style: UIAlertActionStyle.default, handler:{(alert:UIAlertAction) -> Void in

    presentViewController(ViewController, animated: true, completion: nil)
})

When i try to use presentViewController, this error appears: "Cannot convert value of type "ViewController.Type" to expected argument type 'UIViewController'"

And when I try to use performSegue, I use is this way:

performSegue(withIdentifier: "teste", sender: (Any).self)

And then the following error appears: "Implitcit use of self in closure; use 'self.' to make capture semantics explicit"

Can anyone please help me?

like image 615
Lucas Leal Avatar asked Jul 26 '17 15:07

Lucas Leal


1 Answers

So to fix the presentViewController(ViewController, animated: true, completion: nil) function in the okConfirmAction closure, try this:

self?.present(ViewController(), animated: true, completion: nil)

And for the performSegue(withIdentifier:sender:) function in the okConfirmAction closure, try:

self?.performSegue(withIdentifier: "teste", sender: self)

As it is a closure you have to use self before calling the function. This is to make you aware that you may cause a retain cycle.

Write the closure as follows to prevent the retain cycle (using a weak self reference means we replace self with self? as a prefix for present(_:animated:completion:) and performSegue(withIdentifier:sender:)):

let okConfirmAction = UIAlertAction(title:"Sim", style: UIAlertActionStyle.default, handler:{ [weak self] (alert:UIAlertAction) -> Void in
//insert code from above
})
like image 193
Pranav Kasetti Avatar answered Oct 19 '22 06:10

Pranav Kasetti