gpt4 book ai didi

swift - 如何通过函数外的按钮修改函数?

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

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations[0]

let kmh = String(format: "%.0f",(location.speed*3.6))
let ms = String(format: "%.0f",(location.speed))

speedLabel.text = kmh // change [kmh] to [ms] when the button is tapped

当我点击此按钮 (msBtn) 时,我希望函数 (speedLabel.text) 中的速度标签从 kmh 更改到 ms

@IBAction func msBtn(_ sender: Any) {
// add some magic code here //
}

最佳答案

在类中创建一个 bool 值并更改它。

var shouldDisplayKmh = true

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let speed = String(format: "%.0f",(location.speed * (shouldDisplayKmh ? 3.6 : 1)))
speedLabel.text = speed
}

@IBAction func msBtn(_ sender: Any) {
shouldDisplayKmh = !shouldDisplayKmh
}

关于swift - 如何通过函数外的按钮修改函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238367/

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