gpt4 book ai didi

ios - 何时何地在 Swift 中关闭 UIAlertController?

转载 作者:行者123 更新时间:2023-11-28 11:30:44 26 4
gpt4 key购买 nike

我正在调用一个执行 URLSession 的方法,但在它执行任何操作之前,会显示一个 UIAlertController 阻塞 UI,直到获得来自请求的某种响应。逻辑告诉我,在主线程上调用该方法的完成 block 中关闭 UIAlertController 将是最佳选择。我的假设是错误的吗?显然是这样,因为呈现的 UIAlertController 确实会显示,但永远不会关闭。帮忙?

block :

getCostandIV { output in

let cost = output["ask"] as! NSNumber
let IV = output["IV"] as! NSNumber

self.enteredCost = cost.stringValue
self.enteredIV = IV.stringValue

DispatchQueue.main.async {

self.progress.dismiss(animated: true, completion: nil)
self.tableView.reloadSections(IndexSet(integer: 1), with: UITableView.RowAnimation.none)
self.canWeSave()

}

}

功能:

 func getCostandIV (completionBlock: @escaping (NSMutableDictionary) -> Void) -> Void {

DispatchQueue.main.async {


self.progress = UIAlertController(title: "Retrieving ask price and volatility...", message: nil, preferredStyle: UIAlertController.Style.alert)
self.present(self.progress, animated: true, completion: nil)

}

guard let url = URL(string: "https://api.tdameritrade.com/v1/marketdata/chains?apikey=test&symbol=\(symbol)&contractType=\(type)&strike=\(selectedStrike)&fromDate=\(selectedExpiry)&toDate=\(selectedExpiry)") else {
return
}

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
//print(error?.localizedDescription ?? "Response Error")

DispatchQueue.main.async {


self.presentedViewController?.dismiss(animated: true, completion: {

let alert = UIAlertController(title: "There was an error retrieving ask price and volatility.", message: "Please try again later.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alert, animated: true)

})

}

return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
// //print(jsonResponse) //Response result

guard let jsonDict = jsonResponse as? NSDictionary else {
return
}
// //print(jsonDict)

var strikeMap : NSDictionary = [:]

if self.type == "CALL" {
strikeMap = jsonDict["callExpDateMap"] as! NSDictionary

} else {
strikeMap = jsonDict["putExpDateMap"] as! NSDictionary

}

self.strikes.removeAllObjects()

let inner = strikeMap.object(forKey: strikeMap.allKeys.first ?? "<#default value#>") as! NSDictionary
let innerAgain = inner.object(forKey: inner.allKeys.first ?? "<#default value#>") as! NSArray
let dict : NSDictionary = innerAgain[0] as! NSDictionary

let dict2 = ["ask" : dict["ask"] as! NSNumber, "IV" : dict["volatility"] as! NSNumber] as NSMutableDictionary



completionBlock(dict2)


} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}

编辑:使用self.presentedViewController?.dismiss(animated: true, completion: nil) 没有解决问题。此外,未调用 self.progress 的 dismiss 函数的完成 block 。

编辑 2:presentedViewController 就在关闭之前,回调中的代码为 nil,即使在关闭之前在警报 Controller 上调用了 present?

最佳答案

使用这个,为了消除你的警报,你应该在异步 block 中添加 dismiss 方法,并为此设置定时器,你应该告诉异步 block 从现在到 5 秒开始异步,然后做一些事情:

        alert.addAction(UIAlertAction(title: "ok", style: .default,
handler: nil))
viewController.present(alert, animated: true, completion: nil)

// change to desired number of seconds (in this case 5 seconds)
let when = DispatchTime.now() + 5
DispatchQueue.main.asyncAfter(deadline: when){
// your code with delay
alert.dismiss(animated: true, completion: nil)
}

关于ios - 何时何地在 Swift 中关闭 UIAlertController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800413/

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