【问题标题】:How to dismiss an UIAlertController with no actions in SWIFT?如何在 SWIFT 中不执行任何操作就关闭 UIAlertController?
【发布时间】:2014-12-06 17:11:22
【问题描述】:

我在下载图像时显示 UIAlertController。下载完成后,我想推送一个视图控制器。我在控制台中有一个错误,因为我没有关闭警报控制器:

pushViewController:animated: called on <UINavigationController 0x7fb190c6ee00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

在我的主视图控制器中,当下载完成后,我推送另一个视图:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //....

    var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
    self.presentViewController(alert, animated: true, completion: nil)


    dispatch_async(dispatch_get_main_queue(), {
        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
               self.performSegueWithIdentifier("postview", sender: self)

        }
   })
}

我试过用dismissViewControllerAnimated,但我有完全相同的错误:

dispatch_async(dispatch_get_main_queue(), {
            if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                   alert.dismissViewControllerAnimated(true, completion: nil)
                   self.performSegueWithIdentifier("postview", sender: self)

            }
       })

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    在前一个视图控制器被解除之前,您不应该调用performSegueWithIdentifier。要正确计时,请从完成处理程序中执行:

    dispatch_async(dispatch_get_main_queue(), {
        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
           alert.dismissViewControllerAnimated(true, completion: {
               self.performSegueWithIdentifier("postview", sender: self)
           })
        }
    })
    

    现在执行 segue 的调用在解雇结束之前不会开始,从而防止您看到错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2016-04-30
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多