【发布时间】:2015-06-17 07:24:38
【问题描述】:
实际上,我有一个具有 4 种方法的视图控制器。 3 种方法用于显示UIAlertController,没有“确定”或“取消”等操作按钮。仅标题和消息。解雇那些UIAlertController 的最终方法。我的警报之一是在请求异步调用时显示。当我们得到结果时,它会关闭并隐藏。
问题是当我的第一个警报正在显示时突然连接丢失,而另一个将显示“Internet Lost”消息的警报由于该警告而无法显示。
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!
这意味着我无法成功解除第一个警报。有什么帮助吗?
这是我的警报控制器
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)
}
}
这是我的视图控制器(注意我不会显示 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()
}
}
})
}
【问题讨论】:
标签: ios swift uialertcontroller