ios - 链接 UIAlertAction 打开另一个 ViewController

标签 ios swift uiviewcontroller uialertview uialertcontroller

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

            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)

实际上几天前我遇到了这样的问题,但我无法正确理解答案。那么谁能解释一下为什么会发生这种情况并且不会给出任何错误

最佳答案

您的 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 类可能有问题。

关于ios - 链接 UIAlertAction 打开另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33367967/

相关文章:

swift - 如何获取Int值的所有数字

html - 以编程方式调整 wkwebview 的高度

ios - 通过按钮以编程方式将 UITableViewController 替换为 UIViewController,并使用里面的 tableView

ios - 获取文档目录 iOS 中的项目数组

iOS PayUMoney SDK - 移动 sdk 哈希的支付相关详细信息丢失

ios - 如何在 Objective-C 、iOS 中将 NSMutableArray 数组分成两个单独的数组或两个可变数组

arrays - 在 Swift 中创建 JSON 数组以更新 MySQL 数据库

ios - 无法安装 Cocoapods - 在项目目录中找不到 podfile

ios - viewWillAppear ext 的 JASidePanels 委托(delegate)未被调用

ios - 在 CNContactViewController 中预加载/显示数据(swift)