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

前端 未结 2 1594
后悔当初
后悔当初 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
    })
    

提交回复
热议问题