I have a UISlider with value from 0 to 8 (meters). I try to convert from Meters to CM, Feet, Inches and to display this values on 2 UILabels. In one label I want to show the value like: 19 ft 10" and in another label I want to show the value like: 6 m 4 cm. I'm struggling with the conversion and maybe someone can help me display this values.
Here is my code in the 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)
}
}
Here is my code in the 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)"
}
}
The result needs to look like this:
Thanks if you read this !