user1871869
user1871869

Reputation: 3367

Make UIAlertViewController action unclickable?

I want to make a UIAlertAction unclickable. Basically I have an UIAlertView that pops up when I try to download something. When this download finishes, I want the UIAlertView's action to go from being unclickable to clickable. When it becomes clickable, it means that the download has finished. Here's what I have so far:

@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()
}

Everything works but I can't seem to find a way to get the action to be clickable only when the animation has finished. I tried adding the action to the UIAlertViewController and I present the UIAlertViewController again when the download finished but I get an error saying that I can't present something when an UIAlertViewController is already active. Any help would be appreciated. Thanks!

Upvotes: 1

Views: 318

Answers (1)

André Slotta
André Slotta

Reputation: 14030

generally i would recommend against using an alertview as a placeholder for such a task. but to give you an idea take a look at this:

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
    }
}

where that delay part

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
}

should be replaced by your fetching logic.

good luck!

Upvotes: 1

Related Questions