Actually I have one view controller with 4 methods. 3 methods use to display UIAlertController
with no action button like "ok" or "cancel". Title and message only. The final methods for dismissing those UIAlertController
. One of my alert is show when async call is requesting. When we got the results,it will dismiss and hide.
The problem is while my first alert is displaying and suddenly connection lost and the other alert which will display about "Internet Lost" message cant display because of that warning.
2015-06-17 13:49:04.787 myauction[2404:40506] Warning: Attempt to present <UIAlertController: 0x7f8d136bafa0> on <myauction.AuctionLatestViewController: 0x7f8d13771180> while a presentation is in progress!
Which mean i cant dismiss the first alert successfully. Any help please?
Here is myAlertController
import UIKit
class MyAlertViewController:UIViewController{
var myAlertController : UIAlertController!
func displayLoadingAlert(viewController: UIViewController?) -> UIAlertController {
var controllerToPresent = viewController
if controllerToPresent == nil {
controllerToPresent = self
}
//create an alert controller
myAlertController = UIAlertController(title: "Loading...", message: "We are getting the data,Please Wait", preferredStyle: .Alert)
let indicator = UIActivityIndicatorView()
indicator.color = UIColor.redColor()
indicator.setTranslatesAutoresizingMaskIntoConstraints(false)
myAlertController.view.addSubview(indicator)
let views = ["pending" : myAlertController.view, "indicator" : indicator]
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[indicator]-(7)-|", options: nil, metrics: nil, views: views)
constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|[indicator]|", options: nil, metrics: nil, views: views)
myAlertController.view.addConstraints(constraints)
indicator.userInteractionEnabled = false
indicator.startAnimating()
controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)
return myAlertController
}
func connectionErrorAlert(viewController: UIViewController?) -> UIAlertController {
var controllerToPresent = viewController
if controllerToPresent == nil {
controllerToPresent = self
}
//create an alert controller
myAlertController = UIAlertController(title: "Not Connected", message: "No Internet Connection", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
myAlertController.addAction(defaultAction)
controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)
return myAlertController
}
func requestTimeOutErrorAlert(viewController: UIViewController?) -> UIAlertController {
var controllerToPresent = viewController
if controllerToPresent == nil {
controllerToPresent = self
}
//create an alert controller
myAlertController = UIAlertController(title: "Request Time Out", message: "Please Tap Retry to Get The Data", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil)
myAlertController.addAction(defaultAction)
controllerToPresent!.presentViewController(myAlertController, animated: true, completion: nil)
return colayAlertController
}
func dismissLoadingAlert(){
myAlertController.dismissViewControllerAnimated(true, completion: nil)
}
}
Here is my viewcontroller(note i will not show initialization of myAlertViewController)
func getResults{
api.searchCar()
//This is where i start to load my first alert every time it call
self.myAlertViewController = displayLoadingAlert(self)
}
func didReceiveAPIResults(results: NSDictionary,headers:JSON) {
dispatch_async(dispatch_get_main_queue(), {
//do about showing the results....
//Then i dismiss my first loading alert
self.myAlertController.dismissViewControllerAnimated(true){}
})
}
func didNotReceiveAPIResults(results: Bool,error:NSError){
dispatch_async(dispatch_get_main_queue(), {
//I did dismiss the first alert before i gonna show another one
self.myAlertController.dismissViewControllerAnimated(true){
if (results) {
if error.localizedDescription == "The Internet connection appears to be offline."{
self.myAlertViewController = connectionErrorAlert(self)
self.carTableView.hidden=true
self.retryButton?.hidden=false
self.retryButton?.enabled=true
}
if error.localizedDescription == "Request Time Out."{
self.myAlertViewController = requestTimeOutErrorAlert(self)
self.carTableView.hidden=true
self.retryButton?.hidden=false
self.retryButton?.enabled=true
}
}else{
//self.myAlertController.displayLoadingAlert(self)
self.retryButton?.hidden=true
self.retryButton?.enabled=false
self.carTableView.hidden=false
self.carTableView.reloadData()
}
}
})
}