I have a UIStepper
with the following parameters:
value: 40
minimumValue: 35
maximumValue: 45
I want to update the UILabel
(linked to it) at app' launch in order to make the it displays value
. It's currently displaying the minimumValue
by default and I don't know why. Any ideas ?
IBOutlets
first (UIStepper
& UILabel
) related to the size valueshowSizeLabel()
function and called it when the app is launch in viewDidLoad()
UIStepper
was updated.Here is the code for my ViewController.swift
file :
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sizeLbl: UILabel!
@IBOutlet weak var sizeStepper: UIStepper!
var size: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Sets the first UI's display
showSizeValue()
}
// Updates in real time the shoe size displayed on screen
func showSizeValue() {
sizeLbl.text = String(Int(sizeStepper.value))
size = sizeLbl.text ?? ""
updateShoeImage()
}
// Stepper pressed
@IBAction func sizeChanged(_ sender: UIStepper) {
showSizeValue()
updateShoeImage() // Update the shoe image if size is pressed
updateOrderResult() // Update order result's message when size is pressed
}
}
This is an iOS bug. The value
property of the stepper set in the storyboard is not honored unless the minimum value is set to 0.
For example, if you setup the stepper in the storyboard with a min value of 0, a max value of 100, a value of 50, and a step of 1, then when the view controller loads, the stepper will show a value of 50 as expected.
But if you change the min value to 1 and leave everything else the same, then the value will show as 1 instead of 50 when the view controller loads.
The simple work around is to set the initial value in code.
override func viewDidLoad() {
super.viewDidLoad()
sizeStepper.value = 40 // work around iOS bug
// Sets the first UI's display
showSizeValue()
}