gpt4 book ai didi

swift - 如何将米和厘米转换为英尺和英寸并在 UILabel 上显示它们

转载 作者:行者123 更新时间:2023-11-28 13:46:28 57 4
gpt4 key购买 nike

<分区>

我有一个值从 0 到 8(米)的 UISlider。我尝试将米从米转换为厘米、英尺、英寸,并在 2 个 UILabel 上显示此值。在一个标签中,我想显示这样的值:19 英尺 10",在另一个标签中,我想显示这样的值:6 米 4 厘米。我正在努力进行转换,也许有人可以帮助我显示这些值。

这是我在 View 中的代码:

protocol VehicleHeightCellDelegate {
func vehicleHeightSliderValueChanged(_ slider: UISlider, _ feetsLabel: UILabel, _ metersLabel: UILabel)
}

class VehicleHeightCell: UITableViewCell {

// Interface Links
@IBOutlet weak var vehicleHeightSlider: UISlider!
@IBOutlet weak var vehicleHeightFeetsLabel: UILabel!
@IBOutlet weak var vehicleHeightMetersLabel: UILabel!

// Properties
var delegate: VehicleHeightCellDelegate?

@IBAction func vehicleHeightValueChanged(_ sender: UISlider) {

delegate?.vehicleHeightSliderValueChanged(vehicleHeightSlider, vehicleHeightFeetsLabel, vehicleHeightMetersLabel)
}
}

这是我在 Controller 中的代码:

extension ChecklistInspectionController: VehicleHeightCellDelegate{

func vehicleHeightSliderValueChanged(_ slider: UISlider, _ feetsLabel: UILabel, _ metersLabel: UILabel) {

let currentValue = Int(slider.value)

let heightMeters = Measurement(value: Double(currentValue), unit: UnitLength.meters)
let heightFeet = heightMeters.converted(to: UnitLength.feet)

let heightCentimeters = heightMeters.converted(to: UnitLength.centimeters)
let heightInches = heightMeters.converted(to: UnitLength.inches)


feetsLabel.text = "\(heightFeet)" + " \(heightInches)"
metersLabel.text = "\(heightMeters)" + " \(heightCentimeters)"
}
}

结果需要如下所示:

enter image description here

目前看起来像这样: enter image description here

感谢您阅读本文!

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