我应该如何发出快速超时的警报

时间:2015-05-18 11:05:48

标签: swift uialertcontroller

我使用UIAlertController来显示需要用户操作的用户提醒。我想跳过用户操作并在一段时间后(例如10秒)使警报消失。这样做的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

创建UIAlertViewController后尝试此操作

var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(10.0 * Double(NSEC_PER_SEC))) 
dispatch_after(dispatchTime, dispatch_get_main_queue(), { 
    yourAlertViewController.dismissViewControllerAnimated(true, completion: nil)   
})

答案 1 :(得分:1)

这对你来说非常合适

    //Define & Initialize Alert (Example)
    var alert: UIAlertController = UIAlertController();      

    // Delay the execution (Dismiss AlertController) by 10 seconds
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        alert.dismissViewControllerAnimated(true, completion: nil)
    });