【问题标题】:Link UIAlertAction to open another ViewController链接 UIAlertAction 以打开另一个 ViewController
【发布时间】:2015-10-27 12:25:02
【问题描述】:

基本上我有一个 uialertaction 需要在按下时显示另一个视图控制器。起初我是这样做的,但现在即使我没有收到错误代码,应该打开的视图控制器也只是一个黑页。

            if((user) != nil) {

                let alert = UIAlertController(title: "Succesfull", message: "Logged In", preferredStyle: UIAlertControllerStyle.Alert)

                    self.presentViewController(alert, animated: true, completion: nil)

                        let delay = 2.0 * Double(NSEC_PER_SEC)

                            let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
                            dispatch_after(time, dispatch_get_main_queue(), {

                            alert.dismissViewControllerAnimated(true, completion: { () -> Void in

                                let logInViewController = BoardViewController()

                                    self.presentViewController(logInViewController, animated: true, completion: nil)

实际上几天前我遇到了这样的问题,但我无法正确理解答案。所以谁能解释我为什么会发生并且没有给出任何错误

【问题讨论】:

    标签: ios swift uiviewcontroller uialertview uialertcontroller


    【解决方案1】:

    您的 AlertController 没有任何操作。您只是在显示 AlertController 2 秒后呈现 logInViewController

    您可以像这样向 AlertController 添加操作,而不是 dispatch_after

    if(user != nil) {
          let alert = UIAlertController(title: "Succesful", message: "Logged In", preferredStyle: UIAlertControllerStyle.Alert)
          let action = UIAlertAction(title: "OK", style: .Cancel, handler: { // Also action dismisses AlertController when pressed.
                action in
    
                  let logInViewController = BoardViewController()
                  self.presentViewController(logInViewController, animated: true, completion: nil) // When press "OK" button, present logInViewController
    
                    }
            )
          alert.addAction(callAction)// add action to alert
          self.presentViewController(alert, animated: true, completion: nil)// show AlertController
    }
    

    如果你从 BoardViewController 得到空白页,那么你的 BoardViewController 类可能有问题。

    【讨论】:

    • 非常感谢您的回答。是的,在我理解之前,我的代码根本没有任何意义。它工作正常。我还按照您的建议编辑了 BoardViewController 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多