Reputation: 49
I'm trying to show a loading indicator while loading the data:
let alert = UIAlertController(title: "Updating data", message: "Please wait...", preferredStyle: .alert)
alert.view.tintColor = UIColor.black
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10,y: 5,width: 50, height: 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
self.present(alert, animated: true, completion: nil)
Then if the data loading is completed, I want to show another alert telling the user that the data is completed:
if dataLoaded{
//dismiss the previous alert then show the new one
dismiss(animated: false, completion: nil)
let alert2 = UIAlertController(title: "Alert", message: "Data has been updated", preferredStyle: UIAlertControllerStyle.alert)
alert2.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert2, animated: true, completion: nil)
}
The 2nd alert never gets shown, any thought. I'm fairly new to swift
Thanks in advance BT
Upvotes: 0
Views: 2158
Reputation: 2286
Use completion block to show next alert. Try this code, this is working.
let alert = UIAlertController(title: "Updating data", message: "Please wait...", preferredStyle: .alert)
alert.view.tintColor = UIColor.black
let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10,y: 5,width: 50, height: 50)) as UIActivityIndicatorView
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
self.present(alert, animated: true) {
self.dismiss(animated: true, completion: {
let alert2 = UIAlertController(title: "Alert", message: "Data has been updated", preferredStyle: UIAlertControllerStyle.alert)
alert2.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert2, animated: true, completion: nil)
})
}
Upvotes: 2