I would like to change the UIAlertController's message after it is created. Inside the alert I would like to show the status of download.
@IBAction func dbWillSync(sender: AnyObject) {
alert = UIAlertController(title: "SYNC",
message: "0%",
preferredStyle: .Alert)
let dismissHandler = {
(action: UIAlertAction!) in
self.dismissViewControllerAnimated(true, completion: nil)
}
alert.addAction(UIAlertAction(title: "STOP DOWNLOAD",
style: .Cancel,
handler: dismissHandler))
presentViewController(alert, animated: true, completion: nil)
download();
}
func download(){
for i in 1...100{
alert.message="\(i) %"
sleep(1)
}
}
what is wrong ?
Thanks
* EDIT *****
I do not know if it's the best solution:
let thread = NSThread(target:self, selector:"download", object:nil)
thread.start()
.. but it works!
Improvements are welcome!
In iOS, graphics actions don't happen in real time. The alert is not yet put on the screen when you call your download method. All the graphics happen after you exit your methods and return to the system main loop. Thus when your download method is running, the alert isn't even displayed. You have to update your alert message from the download itself.