【问题标题】:How can I get a UIAlertAction handler to execute before the UIAlertController is dismissed?如何在 UIAlertController 被解除之前执行 UIAlertAction 处理程序?
【发布时间】:2014-11-30 07:15:21
【问题描述】:

我想从 UIAlertAction 调用 segue,但处理程序在 UIAlertController 完成解除之前不会执行。

let editMenu = UIAlertController(title: "Edit", message: nil, preferredStyle: .ActionSheet)

        let myAction = UIAlertAction(title: "My Action", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) -> Void in
                tableView.setEditing(false, animated: true)

            self.performSegueWithIdentifier("some_segue", sender: self)

        })
editMenu.addAction(myAction)
self.presentViewController(editMenu, animated: false, completion: nil)

我希望处理程序立即执行。

【问题讨论】:

    标签: ios swift xcode6


    【解决方案1】:

    AFAIK 只需在您显示/呈现警报后编写它,因为线程不会等待它回来:

    // Create the controller
    let editMenu = UIAlertController(title: "Edit", message: nil, preferredStyle: .ActionSheet)
    // Create an action for the controller with no handler
    let myAction = UIAlertAction(title: "My Action", style: UIAlertActionStyle.Default, handler: nil)
    // Add the action to the controller
    editMenu.addAction(myAction)
    
    // show the controller/Alert to the user
    self.presentViewController(editMenu, animated: false, completion: nil)
    
    // As the above calls won't wait for the user these lines will be executed immediately
    tableView.setEditing(false, animated: true)
    self.performSegueWithIdentifier("some_segue", sender: self)
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多