【问题标题】:UIAlertController to go to new ViewControllerUIAlertController 去新的 ViewController
【发布时间】:2017-02-26 23:08:57
【问题描述】:

我已经生成了一个 UIAlert,我想链接它来打开一个新的 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)
}

基本上,当用户单击正确答案时,会弹出警报,然后当用户单击“确定”时,我希望在应用程序的下一部分弹出一个新的视图控制器。当前发生的所有事情是当我单击警报消息中的“确定”时屏幕变黑了?

任何帮助将不胜感激..

【问题讨论】:

    标签: swift uiviewcontroller uialertcontroller


    【解决方案1】:

    在 okButton 的完成处理程序中设置你想去的 viewController。

    这是我使用的一个例子。它是一个我可以从任何地方调用的类函数,我已经硬编码了我想去的视图控制器,它是导航控制器的一部分,只需将“LogInNavCont”更改为您的视图控制器的情节提要 ID,并将 UINavigationController 更改为 UIViewController(如果它是独立的视图控制器。

    如果不想作为类函数使用,只需将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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2017-12-23
      • 2016-10-20
      • 1970-01-01
      • 2019-06-01
      • 2019-12-25
      相关资源
      最近更新 更多