我試圖做一個警報控制器,其中,如果答案是「好」,那麼它將執行一個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)
然後出現以下錯誤:「在關閉中隱藏自我使用;使用'自我'。使捕獲的語義明確的」
誰能幫幫我嗎?
我假設的ViewController是類名,而不是類實例 - 因此問題 – Miknash
@Miknash請你解釋一下多一點?我是新來的swift:p –
你有類ViewController嗎?或者你有var ViewController:MyViewController? – Miknash