链接UIAlertAction以打开另一个ViewController

时间:2015-10-27 12:25:02

标签: ios swift uiviewcontroller uialertview uialertcontroller

基本上我有一个uialertaction,需要在按下时显示另一个viewcontroller。起初我做了但现在即使我没有得到错误代码,应该打开的viewcontroller只是一个黑页。

            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)

实际上几天前我运行了这样的问题,但我无法理解答案。所以任何人都可以解释为什么它会发生并且不会发生。给出任何错误

1 个答案:

答案 0 :(得分:2)

您的AlertController没有任何操作。您只需在2秒后显示 logInViewController ,即可显示AlertController。

您可以像这样向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类中可能有问题。