本文介绍了无法转换类型为"ViewController.Type"的值到预期的参数类型"UIViewController";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个警报控制器,如果答案为"Ok",它将在其中执行对MapView的选择.这是完整的代码:

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

我在这部分遇到麻烦

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

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

当我尝试使用presentViewController时,出现此错误:无法将类型"ViewController.Type"的值转换为预期的参数类型"UIViewController""

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

当我尝试使用performSegue时,我使用的是这种方式:

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

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

然后出现以下错误:在封闭中隐式使用self;使用'self'.使捕获语义明确"

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

有人可以帮助我吗?

推荐答案

因此,要在 okConfirmAction 中修复 presentViewController(ViewController,animation:true,complete:nil)函数>关闭,请尝试以下操作:

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

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

对于 okConfirmAction 闭包中的 performSegue(withIdentifier:sender:)函数,请尝试:

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

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

由于它是一个闭包,因此您必须在调用函数之前使用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.

按如下所示编写闭包以防止保留周期(使用弱的 self 引用意味着我们将 self 替换为 self?作为前缀)对于 present(_:animated:completion:) performSegue(withIdentifier:sender:)):

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

这篇关于无法转换类型为"ViewController.Type"的值到预期的参数类型"UIViewController";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:45