gpt4 book ai didi

ios - PrepareForSegue 对于我的其他函数来说太快了,变量没有及时更新?

转载 作者:行者123 更新时间:2023-11-28 10:19:36 25 4
gpt4 key购买 nike

我有一个带有按钮的 FIRSTViewController。该按钮有一个 IBAction 函数,它在其中调用 CLGeocoder()。该按钮也是 SECONDViewController 的一个 segue。

IBAction 中的 CLGeocoder 更新两个变量,这些变量在 prepareForSegue 函数中传递。

但是,由于 CLGeocoder 异步操作,在 segue 发生之前更新变量太慢,因此,不正确的变量被传递给 SECONDViewController。我该如何解决这个问题?

这是我的 IBAction 方法中的代码:

        geocoder.geocodeAddressString(userInputText!) { (placemarks, error) -> Void in
if let firstPlacemark = placemarks?[0] {
self.searchLatitude = "\(firstPlacemark.location!.coordinate.latitude)"
self.searchLongitude = "\(firstPlacemark.location!.coordinate.longitude)"
self.addressLoaded = true
} else {
//implement error pop up
print("error")
}
}

最佳答案

所有 CLGeocoder 实例方法都接受一个 completionHandler 参数。尝试从中调用 performSegue

编辑:这是一个例子

@IBAction private func buttonPressed(sender: UIButton?) {

let geoCoder = CLGeocoder()

geoCoder.geocodeAddressString(
"Your address string") { (let placeMarks, let error) in

if let error = error {

// handle the error

} else if let placemarks = placeMarks {

// Do whatever with the placemarks
self.performSegueWithIdentifier(
"Segue identifier",
sender: sender
)
}
}
}

关于ios - PrepareForSegue 对于我的其他函数来说太快了,变量没有及时更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738354/

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