现在,当我移动滑块时,我的文本字段的值正在改变。我还需要向后工作,改变文本字段值,滑块移动。
我的代码:
- (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;
}
答案 0 :(得分: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/391040和https://stackoverflow.com/a/4711136/391040。
答案 1 :(得分: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];