iOS 关闭 UIAlertController 以响应事件 [英] iOS dismiss UIAlertController in response to event

查看:22
本文介绍了iOS 关闭 UIAlertController 以响应事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示一个 UIAlertController 以便在显示我的主要 ViewController 之前等待一个事件(来自第三方的异步数据请求)完成> 与用户交互.

I have a situation where I want to present a UIAlertController in order to wait for an event (asynchronous request for data from third party) to finish before showing my main ViewController to the user to interact with.

异步代码完成后,我想关闭 UIAlertController.我知道通常 UIAlertControllers 设置有一个按钮来关闭它,这是来自用户的输入.我想知道我想要做什么(用事件而不是用户输入关闭)是否可行?

Once the asynchronous code finishes, then I want to dismiss the UIAlertController. I know that normally UIAlertControllers are setup with a button to dismiss it, which is input from the user. I am wondering if what I want to do (dismiss with an event instead of user input) is possible?

到目前为止,我尝试显示 UIAlertController,然后在 while 循环中等待,检查事件发生时的布尔值:

So far, I tried to show the UIAlertController, and then wait in a while loop checking a boolean for when the event occurs:

var alert = UIAlertController(title: "Please wait", message: "Retrieving data", preferredStyle: UIAlertControllerStyle.Alert)

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

// dataLoadingDone is the boolean to check   
while (!dataLoadingDone) {

}

self.dismissViewControllerAnimated(true, completion: nil)

这是一个警告

警告:在演示或关闭正在进行时尝试从视图控制器中关闭!

Warning: Attempt to dismiss from view controller while a presentation or dismiss is in progress!

并且不会关闭 UIAlertController.我也尝试过 alert.dismissViewControllerAnimated(true, completion: nil) 而不是 self.dismissViewControllerAnimated(true, completion: nil),但这并没有摆脱 UIAlertController 要么.

and does not dismiss the UIAlertController. I also tried alert.dismissViewControllerAnimated(true, completion: nil) instead of self.dismissViewControllerAnimated(true, completion: nil), but this doesn't get rid of the UIAlertController either.

推荐答案

对于 dataLoadingDone<,我不会使用 while 循环,而是使用 didSet 观察者/代码> 属性.因此,您可以尝试类似以下代码:

I wouldn't use a while loop but a didSet observer for your dataLoadingDone property. Thereby, you may try something similar to the following code:

class ViewController: UIViewController {

    var dismissAlertClosure: (() -> Void)?
    var dataLoadingDone = false {
        didSet {
            if dataLoadingDone == true {
                dismissAlertClosure?()
            }
        }
    }


    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        let alert = UIAlertController(title: "Please wait", message: "Retrieving data", preferredStyle: .Alert)
        presentViewController(alert, animated: true, completion: nil)

        // Set the dismiss closure to perform later with a reference to alert
        dismissAlertClosure = {
            alert.dismissViewControllerAnimated(true, completion: nil)
        }

        // Set boolValue to true in 5 seconds in order to simulate your asynchronous request completion
        var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(5.0 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue(), { self.dataLoadingDone = true })
    }

}

这篇关于iOS 关闭 UIAlertController 以响应事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆