gpt4 book ai didi

ios - 在 ibaction 后触发 segue

转载 作者:行者123 更新时间:2023-11-30 11:43:18 25 4
gpt4 key购买 nike

我正在尝试在 IBAction 发生后执行转场。这是我的代码,正如您所看到的,当我按下按钮时,我使用 alamofire 发出获取请求。问题是,该请求(据我所知)是一个异步方法,因此如果 getPlayer 方法没有完成它应该做的事情,segue 将展开并执行 eve。我可以解决这个问题的唯一方法是将 perfomrsegue 方法放入 if 语句中,在其中检查 person.name 的值,但我必须按两次按钮,我就是不知道如何解决这个问题!

@IBAction func getPlayerPressed(_ sender: UIButton) {

userDefaults.set(tagTextField.text!, forKey: "userTag")
let userTag = userDefaults.string(forKey: "userTag")
getPlayerData(with: userTag!)
performSegue(withIdentifier: "goToPlayerProfile", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destionationVC = segue.destination as! PlayerProfileViewController
destionationVC.playerName = player.name
print(destionationVC.playerName)
print("prepared for segue")
}


func getPlayerData (with tag: String) {
let finalURL = baseURL + tag
Alamofire.request(finalURL, headers: headers).responseJSON { (response) in
if response.result.isSuccess {
print("Got player data!")
let playerJSON = JSON(response.result.value!)
self.player.name = playerJSON["name"].stringValue
print(self.player.name)
} else {
print("Error: \(response.result.error!)")
}
}

最佳答案

异步alamofire请求完成后执行segue。

func getPlayerData (with tag: String) {
let finalURL = baseURL + tag
Alamofire.request(finalURL, headers: headers).responseJSON { (response) in
if response.result.isSuccess {
print("Got player data!")
let playerJSON = JSON(response.result.value!)
self.player.name = playerJSON["name"].stringValue
print(self.player.name)
DispatchQueue.main.async {
self.performSegue(withIdentifier: "goToPlayerProfile", sender: self)
}
} else {
print("Error: \(response.result.error!)")
}
}
}

关于ios - 在 ibaction 后触发 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49102119/

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