How to set value of UISlider from a UITextField

2020-07-27 19:28发布

问题:

I am using Xcode 4.3 and I want to be able to edit the value of a UISlider using a UITextField. How can I do this? I have a slider that displays its value in a text field, but can I also set the slider's value from a text field?

Thanks for the help!!

回答1:

Assuming "textField" is the name of your text field, I would add a target to it in your viewDidLoad method:

[textField addTarget:self
              action:@selector(textFieldDidChange)
    forControlEvents:UIControlEventEditingChanged];

And then handle the change like you said:

- (void)textFieldDidChange {

    Slider.value = [[textfield.text] intValue];

}

Hope this helps.