Reputation:
I have a textfield on a form where the value changes based on the slider. I am trying update the functionality so that when the user enters a value in the text field the slider will move to the correct position. My UISlider will be a value between 0 and 3.
I have been trying to use this post to help me but, I have not been able to get it to work: How to set value of UISlider from a UITextField
Cheers,
I want the user to be able to use the slider or keyboard to enter the rate discount - but if they enter a value I want the slider to move to the right spot. Using the code given the app crashes. I think it's because of conflicting code. I have been linking the sider as an outlet. Is this correct?
Upvotes: 0
Views: 1482
Reputation: 25261
Are you failing because yoru link is an Objective C version? So here is the swift equivalent. First, add a listener on your textfield like this
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in your text field text change function, set value for your slider
func textFieldDidChange(textField: UITextField) {
if let stringValue = textField.text{
if let intValue = Int(stringValue){
slider.setValue(intValue, animated: true)
}
}
}
Upvotes: 1