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

前端 未结 2 1644
后悔当初
后悔当初 2021-02-20 08:27

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         


        
相关标签:
2条回答
  • 2021-02-20 08:53

    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
    })
    
    0 讨论(0)
  • 2021-02-20 09:14

    Use performSegue(withIdentifier: "teste", sender: self). I'm not sure what you're trying to achieve with the (Any). before self, since self on a class name returns the type of the class, not an instance of the class.

    0 讨论(0)
提交回复
热议问题