使UIAlertViewController操作无法点击?

时间:2015-04-26 07:51:09

标签: ios swift uialertview uialertcontroller

我想让UIAlertAction无法点击。我尝试下载的时候基本上会弹出一个UIAlertView。下载完成后,我希望UIAlertView's action从无法点击变为可点击。当它变为可点击时,表示下载已完成。这就是我到目前为止所拥有的:

@IBOutlet var activityView: UIActivityIndicatorView!

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

override func viewDidLoad(){
    self.activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    self.activityView.center = CGPointMake(130.5, 65.5)
    self.activityView.color = UIColor.blackColor()
}

@IBAction func importFiles(sender: AnyObject){
    self.alert.view.addSubview(activityView)
    self.presentViewController(alert, animated: true, completion: nil)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    //Somehow here I want to make it so that the UIAlertAction is unclickable.
    self.activityView.startAnimating()
}

一切正常但我似乎无法找到一种方法,只有在动画结束时才能让动作可点击。我尝试将操作添加到UIAlertViewController,然后在下载完成时再次显示UIAlertViewController但我收到一条错误消息,说明UIAlertViewController已经存在,我无法提供内容活性。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

通常我建议不要使用alertview作为此类任务的占位符。但是为了给你一个想法,看看这个:

lazy var alert: UIAlertController = {
    var alert = UIAlertController(title: "Please wait...", message: "Please wait for some seconds...", preferredStyle: .Alert)
    alert.addAction(self.action)
    return alert
}()
lazy var action: UIAlertAction = {
    var action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
    action.enabled = false
    return action
}()

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

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

    let delayInSeconds = 3.0
    let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
    dispatch_after(popTime, dispatch_get_main_queue()) { () -> Void in
        self.action.enabled = true
    }
}

那个延迟部分

let delayInSeconds = 3.0
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) { () -> Void in
    self.action.enabled = true
}
应该用你的提取逻辑替换

祝你好运!