2017-07-26 107 views
2

我試圖做一個警報控制器,其中,如果答案是「好」,那麼它將執行一個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

回答

1

所以以修復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

0

使用performSegue(withIdentifier: "teste", sender: self)。我不知道你想實現與(Any).self之前什麼,因爲self上一個類名返回。輸入類,而不是類的實例

+0

我使用(任何)。因爲控制檯告訴我這麼做,老實說(對於這種語言來說有點新鮮)......當我只用自己的時候,它會迴應我:「封閉中使用'self';使用'self'。明確捕捉語義「。 –

+0

啊。只有當你在一個區塊時纔會發生這種情況,而且你通常只需要在自己的變量前加上'self?.'。 – NRitH

+0

是的,它工作:)謝謝 –

相關問題