1

现在,当我移动滑块时,我的文本字段的值正在改变。我也需要向后工作,改变文本字段值,滑块移动。

我的代码:

- (void)sliderChanged:(UISlider *)sender {

    self.slider.valueChangedHandler = ^(id sender) {
    self.aleField.text = [NSString stringWithFormat:@"%.f", [(UISlider *)sender value]];
};

}

编辑

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   slider.value = [aleField.text floatValue];

    NSUInteger newLength = [aleField.text length] + [string length] - range.length;
    return (newLength > 2) ? NO : YES;

}
4

2 回答 2

3

创建文本字段后,添加:

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

然后有以下方法:

- (void)textFieldDidChange:(UITextField*)sender {
    //Assuming slider is a property
    slider.value = [sender.text floatValue];
}

我引用了 https://stackoverflow.com/a/7010676/391040https://stackoverflow.com/a/4711136/391040

于 2012-07-02T08:27:06.340 回答
2

要在键入时执行此操作,请实现文本字段委托

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSCharacterSet *numberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:numberSet] componentsJoinedByString:@""];
    BOOL basicTest = [string isEqualToString:filtered];
    if (basicTest) {
        slider.value = [aleField.text floatValue];
    }
    return basicTest;
}

这也将允许您让用户只插入数字。

要将值反映到滑块:

slider.value = [textField.text floatValue];
于 2012-07-02T08:31:12.767 回答