How to set value of UISlider from a UITextField

2020-07-27 19:01发布

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条回答
Bombasti
2楼-- · 2020-07-27 19:32

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.

查看更多
登录 后发表回答