gpt4 book ai didi

swift - 完成处理程序无法关闭 popAlertController

转载 作者:行者123 更新时间:2023-11-30 11:17:27 24 4
gpt4 key购买 nike

我有一个带有单个 textView 的 View Controller ,我从服务器获取一个字符串并在其上设置。

我在viewDidAppear中调用http-get服务,我需要向等待用户显示弹出警报,直到获取字符串

我做了所有的事情,但我无法在加载警报时关闭 popalert 和应用程序堆栈

    override func viewWillAppear(_ animated: Bool) {
fetchAndPrintEachPerson()

networkReachablity()

popUpLoading() // start showing loading pop alert

getPerson ()

}

override func viewDidLoad() {
super.viewDidLoad()

style()
giftTextFiled.isEnabled = false



}

func popUpLoading(){

popUpAlert = UIAlertController(title: nil, message: "wait ...", preferredStyle: .alert)

let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
loadingIndicator.startAnimating();

popUpAlert.view.addSubview(loadingIndicator)
present(popUpAlert, animated: true, completion: nil)


}

func getPerson(){

guard let url=URL(string: "\(address)person/code") else {return}

print(url)

URLSession.shared.dataTask(with: url) { (data, response, error) in

if let res=response {

//print(res)
}
do {

if let dataContent=data {

let con = try? JSONSerialization.jsonObject(with: dataContent, options: []) as? AnyObject
let data = con??["data"] as? AnyObject
let code = data?["code"] as? String
print(code)
// self.popUpAlert.dismiss(animated: true, completion: {
DispatchQueue.main.async(execute: {
//self.giftTextFiled.text=code!
self.giftTextFiled.isEnabled = true
self.popUpAlert.dismiss(animated: true, completion: nil)

})
//})
}else {
let snack=snackBarAlert()
snack.alert(title: "error", color: UIColor.red)
self.popUpAlert.dismiss(animated: true, completion: nil)
}
}catch let err{
print(err)
DispatchQueue.main.async(execute: {
self.popUpAlert.dismiss(animated: true, completion: nil)

})
}
}.resume()
}

更新第一篇文章。

最佳答案

尝试将其放入 viewDidLoad 并检查是否从服务器收到响应,否则它将通过 else block 并继续显示弹出窗口

关于swift - 完成处理程序无法关闭 popAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51607588/

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