ios 无法将“ViewController.Type”类型的值转换为预期的参数类型“UIViewController”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45331626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:21:18  来源:igfitidea点击:

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

iosswift

提问by Lucas Leal

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:

我正在尝试制作一个警报控制器,其中,如果答案为“确定”,那么它将对 MapView 执行 segue。这是完整的代码:

@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'"

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

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

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

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"

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

Can anyone please help me?

谁能帮帮我吗?

回答by Pranav Kasetti

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

所以要修复闭包中的presentViewController(ViewController, animated: true, completion: nil)函数okConfirmAction,试试这个:

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

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

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

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.

由于它是一个闭包,因此您必须在调用该函数之前使用 self。这是为了让您意识到您可能会导致保留周期。

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

写关闭如下防止保留周期(使用弱self引用意味着我们更换selfself?作为前缀present(_:animated:completion:)performSegue(withIdentifier:sender:)):

let okConfirmAction = UIAlertAction(title:"Sim", style: UIAlertActionStyle.default, handler:{ [weak self] (alert:UIAlertAction) -> Void in
//insert code from above
})

回答by NRitH

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

使用performSegue(withIdentifier: "teste", sender: self). 我不确定你想用(Any).before实现什么self,因为self在类名上返回类的类型,而不是类的实例。