[英]How to dismiss modal ViewController from UIAlertcontroller
[英]How to dismiss uialertcontroller from view completely?
實際上,我有一個具有4種方法的視圖控制器。 使用3種方法來顯示UIAlertController
並且沒有“ ok”或“ cancel”之類的操作按鈕。 僅標題和消息。 消除那些UIAlertController
的最終方法。 當異步呼叫正在請求時,顯示我的警報之一。 當我們得到結果時,它將關閉並隱藏。
問題是當我的第一個警報顯示並且突然連接中斷時,由於該警告,無法顯示有關“ Internet丟失”消息的另一個警報。
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)
}
}
這是我的viewcontroller(注意,我不會顯示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()
}
}
})
}
在以模態方式呈現另一個ViewController
之前,應該將代碼插入dismissViewControllerAnimated
方法的completion
塊中。
看一下您更新的代碼:
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()
}
}
})
}
首先,我犯了一個錯誤,因為我正在為警報創建新的ViewController而不是定義擴展名。因此,如果某些正在搜索結果的初學者不要喜歡我的上述代碼。因此,這就是答案。
如果要添加自定義警報,請使用擴展名。
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
}
}
}
希望對大家有幫助。感謝@ezCoding,它試圖幫助我擺脫我的這個黑暗錯誤,並向我展示成功的光輝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.