其實我有一個視圖控制器4種方法。 3種方法用於顯示UIAlertController
,沒有任何操作按鈕,如「ok」或「cancel」。只有標題和信息。最後的方法是解散那些UIAlertController
。我的一個警報是在異步呼叫請求時顯示的。當我們得到結果時,它會解散並隱藏。如何完全從視圖中解除uialertcontroller?
問題是,當我的第一個警報顯示,突然連接丟失,另一個警報將顯示有關「互聯網丟失」消息不能顯示,因爲該警告。
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!
這意味着我不能關閉第一次警報成功。請幫忙嗎?
這裏是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)
}
}
這裏是我的視圖 - 控制(注意我不會讓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()
}
}
})
}
我收到錯誤,請不要介意再次檢查嗎?謝謝 –
對不起。右括號錯誤。現在檢查線的結束。 – ezcoding
第一次警告「加載」不要解僱,即使當我要顯示另一個alert.Do我需要改變func dismissLoadingAlert(),如果你需要myalertController方法,我可以爲你提供。 –