gpt4 book ai didi

ios - 如何从 View 中完全关闭 uialertcontroller?

转载 作者:行者123 更新时间:2023-11-28 13:12:16 24 4
gpt4 key购买 nike

实际上我有一个带有 4 个方法的 View Controller 。 3 种方法用于显示 UIAlertController 而没有像“确定”或“取消”这样的操作按钮。仅标题和消息。关闭那些 UIAlertController 的 final方法。当请求异步调用时显示我的警报之一。当我们得到结果时,它会消失并隐藏。

问题是当我的第一个警报显示时突然连接断开,另一个将显示“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!

这意味着我无法成功解除第一个警报。有什么帮助吗?

这是我的AlertController

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)
}

}

这是我的 View Controller (注意我不会显示 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 block 中。

看看我更新的代码:

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()
}
}
})
}

关于ios - 如何从 View 中完全关闭 uialertcontroller?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com