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

问题描述:

我试图做一个警报控制器,其中,如果答案是“好”,那么它将执行一个Segue到一个MapView。下面是完整的代码:无法将类型“ViewController.Type”的值转换为预期的参数类型“UIViewController”

@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””

当我尝试使用performSegue,我用的是这样的:

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

然后出现以下错误:“在关闭中隐藏自我使用;使用'自我'。使捕获的语义明确的”

谁能帮帮我吗?

+0

我假设的ViewController是类名,而不是类实例 - 因此问题 – Miknash

+0

@Miknash请你解释一下多一点?我是新来的swift:p –

+0

你有类ViewController吗?或者你有var ViewController:MyViewController? – Miknash

所以以修复okConfirmAction封闭的presentViewController(ViewController, animated: true, completion: nil)功能,试试这个:

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

而且在okConfirmAction封闭的performSegue(withIdentifier:sender:)功能,请尝试:

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

因为它是你必须调用函数之前使用自我封闭。这是为了让你意识到你可能会导致一个保留周期。

写关闭如下防止保留周期(使用弱self参考意味着我们与self?取代selfpresent(_:animated:completion:)前缀和performSegue(withIdentifier:sender:)):

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

它的工作!非常感谢!我使用了你在performSegue中展示给我的功能,它进行得很顺利:) –

+0

@LucasLeal你接受了其他答案 - 如果这一个是正确的,请接受这个 – Miknash

使用performSegue(withIdentifier: "teste", sender: self)。我不知道你想实现与(Any).self之前什么,因为self上一个类名返回。输入类,而不是类的实例

+0

我使用(任何)。因为控制台告诉我这么做,老实说(对于这种语言来说有点新鲜)......当我只用自己的时候,它会回应我:“封闭中使用'self';使用'self'。明确捕捉语义“。 –

+0

啊。只有当你在一个区块时才会发生这种情况,而且你通常只需要在自己的变量前加上'self?.'。 – NRitH

+0

是的,它工作:)谢谢 –