adjusting label to slider value swift

后端 未结 6 1554
南旧
南旧 2021-01-01 14:44

I have a slider and label in my swift project. On the storyboard, I control dragged my slider onto my controller class for that page and created an outlet and also an action

6条回答
  •  隐瞒了意图╮
    2021-01-01 15:09

    If you want to do it programmatically, this is one way.

    var slider: UISlider()
    var label: UILabel()
    
    func numberValueChanged(sender: UISlider) {
        slider.setValue(Float(Int(slider.value)), animated: true)
        updateLabels(slider.value)
    }
    fun updateLabels(nV: Float?) {
        if let v = nV {
            self.label.text = "\(v)"
        }
    }
    
    slider.addTarget (self,
                     action: #selector(numberValueChanged)
                     forControlEvents: UIControlEvents.ValueChanged
    )
    

    Hope this was helpful :)

提交回复
热议问题