2015-06-17 66 views
0

其實我有一個視圖控制器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() 
     } 
     } 
    }) 
} 

回答

2

你應該呈現前插入你的代碼在dismissViewControllerAnimated方法completion塊另一個ViewController模態。

看看你的代碼,我已經更新:

func didNotReceiveAPIResults(results: Bool,error:NSError){ 
    dispatch_async(dispatch_get_main_queue(), { 
     // 
     // I'm calling your error message in the completion block of the 
     // dismissViewControllerAnimated Method 
     // 
     self.myAlertController.dismissViewControllerAnimated(true) { 
     if (results) { 
      if error.localizedDescription == "The Internet connection appears to be offline."{ 
       self.myAlertController.connectionErrorAlert(self) 
      } 

      if error.localizedDescription == "Request Time Out."{ 
       self.myAlertController.requestTimeOutErrorAlert(self) 
      } 

      // 
      // Moved this code out of your if-statements since they are called 
      // no matter which case is true 
      // 
      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() 
     } 
     } 
    }) 
} 
+0

我收到錯誤,請不要介意再次檢查嗎?謝謝 –

+0

對不起。右括號錯誤。現在檢查線的結束。 – ezcoding

+0

第一次警告「加載」不要解僱,即使當我要顯示另一個alert.Do我需要改變func dismissLoadingAlert(),如果你需要myalertController方法,我可以爲你提供。 –

0

首先,我犯了一個錯誤,因爲我創造了新的警報,而不是視圖控制器的定義extension.So,如果一些初學者正在搜索結果,不喜歡我上面的代碼。所以這裏是答案。

如果你想添加自定義提醒,請使用這樣的擴展名。

import UIKit 

extension UIAlertController { 

class func displayLoadingAlert() -> UIAlertController { 

    //create an alert controller 
    let 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() 

    return myAlertController 
} 

class func connectionErrorAlert() -> UIAlertController { 

    let myAlertController = UIAlertController(title: "Not Connected", message: "No Internet Connection", preferredStyle: .Alert) 
    let defaultAction = UIAlertAction(title: "OK", style: .Default,handler:nil) 
    myAlertController.addAction(defaultAction) 
    return myAlertController 
} 

class func requestTimeOutErrorAlert() -> UIAlertController { 
    // same as above 
} 
} 

那麼如何使用它與其他視圖控制器(用於顯示和解聘)

class ViewController : UIViewController{ 

     var myAlert : UIAlertController! 
     //For displaying the one of the alert 
     func getResults(){ 
      myAlert = UIAlertController.displayLoadingAlert() 
      self.presentViewController(myAlert, animated: true, completion: nil) 
     } 
     //For dismissing the alert,please add the code in the completion that will do after dismiss 
     func afterGettingResults(){ 
      self.myAlert.dismissViewControllerAnimated(true){ 
       //do your job after dismiss is done 
      } 
     } 
} 

希望這有助於,everyone.Thanks到@ezCoding它試圖幫助我遠離黑暗錯誤脫身我的,並展示我輕盈的成功。