尝试通过UIAlert以编程方式进行模式移动

时间:2017-09-17 23:34:27

标签: ios swift uialertcontroller

当您按下确定时,我正在尝试通过警报弹出窗口从一个视图转到另一个视图。

问题是我试图使用的代码不是从一个屏幕移动到另一个屏幕。我不确定为什么。

守则:

import UIKit

class ViewController: UIViewController {
    @IBAction func helpButtonPushed(_ sender: UIButton) {
        let alertController = UIAlertController(title: "Help has been sent", message: "Please move towards the Student Services Area. ", preferredStyle: .alert)

        self.present(alertController, animated: true, completion: nil)

        let okAction = UIAlertAction(title: "Ok", style: .default, handler: { (alertAction) in
            //Show the quiz. 
            let storyBoard : UIStoryboard = UIStoryboard(name: "QuizViewcontroller", bundle:nil)

            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Quiz") as! QuizViewController
            self.present(nextViewController, animated:true, completion:nil)

        })

        alertController.addAction(okAction)
    }
}

我尝试使用的两个viewControllers:ViewController和QuizViewController。

View Contollers

提前致谢

2 个答案:

答案 0 :(得分:0)

检查您的视图控制器是否在不使用警报的情况下显示。如果工作正常:首先显示您的警报。我没有看到首先显示警报的代码。 下面的代码工作正常:

@IBAction func helpButtonPushed(_ sender:UIButton){         let alert = UIAlertController(标题:"你好",消息:"你好,你好",preferredStyle:.alert)

    let okAction = UIAlertAction(title: "Ok", style: .default, handler: { (alertAction) in
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
        self.present(nextViewController, animated:true, completion:nil)

    })

    alert.addAction(okAction)

    self.present(alert, animated: true, completion: nil)
}

答案 1 :(得分:0)

对于UIStoryBoard Put QuizViewcontroller不是很好的命名约定使用Main代替默认情况,或者您可以使用项目的部署信息进行检查 - enter image description here

  let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)