2017-02-26 26 views
0

我已經生成了一個UIAlert,我想鏈接這個來打開一個新的viewController。我爲每個不同的viewcontrollers添加了新文件,並且已經鏈接了這個類。UIAlertController去新的ViewController

的代碼,我是...

@IBAction func CorrectButton(_ sender: UIButton) { 

    let refreshAlert = UIAlertController(title: "Correct Answer", message: "Congratulations", preferredStyle: UIAlertControllerStyle.alert) 

    let vc = ThirdViewController() 

    refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in self.present(vc, animated: true, completion: nil) 

    })) 

    present(refreshAlert, animated: true, completion: nil) 
} 

基本上,當用戶點擊了正確答案,當用戶點擊「OK」我希望有一個新視圖控制器以彈出一個警告會彈出,然後應用程序的下一部分。目前發生的所有事情是當我點擊警報消息中的「確定」時,屏幕會變黑。

任何幫助將不勝感激..

回答

0

Instatise你想去的okButton完成處理程序的viewController。

這是我使用的一個例子。它是一個類的函數,我可以從任何地方調用,我已經硬編碼了我想要去的視圖控制器,它是導航控制器的一部分,只需將「LogInNavCont」更改爲您的viewcontrolller和UINavigationController的故事板ID爲UIViewController獨立的viewController。

,如果你不想使用它作爲一類功能,只需更換className.present到self.present

class func createAlertAndGoToLogin(errorTitle: String, errorMessage: String, className: UIViewController) { 
    let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in 

     // go back to the login view controller 
     // go back through the navigation controller 

      let vc = className.storyboard!.instantiateViewController(withIdentifier: "LogInNavCont") as! UINavigationController 
      className.present(vc, animated: false, completion: nil) 


    })) 
    className.present(alert, animated: true, completion: nil) 
} 
相關問題