Iphone 滑块/文本框交换数据

Iphone 滑块/文本框交换数据,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我有一个UISlider,它附带了两个文本框。把它想象成一个小费计算器。考虑一下收据:它有一个位置让你放小费,然后是最终价值。让我们在混合中添加一个滑块 现在我们有两个文本字段(小费百分比和小费金额)和滑块。当滑块移动时,它将调用一个方法,该方法根据用户使用滑块选择的值更新两个文本框 [self.slider addTarget:self action:@selector(sliderValueChanged:) forContro

我有一个
UISlider
,它附带了两个文本框。把它想象成一个小费计算器。考虑一下收据:它有一个位置让你放小费,然后是最终价值。让我们在混合中添加一个滑块

现在我们有两个文本字段(小费百分比和小费金额)和滑块。当滑块移动时,它将调用一个方法,该方法根据用户使用滑块选择的值更新两个文本框

[self.slider addTarget:self 
                    action:@selector(sliderValueChanged:) 
          forControlEvents:UIControlEventValueChanged];

-(void)sliderValueChanged:(id)sender
{
    tipPercent.text = [NSString stringWithFormat:@"%d", (int)slider.value];
    tipPercentDbl = (int)slider.value;//tipPercent as Dbl
    tipDbl = (totalDbl * (tipPercentDbl/100));//tip as Dbl
    tip.text = [NSString stringWithFormat:@"%.2f", tipDbl];
    tipPercent.text = [NSString stringWithFormat:@"%.0f", tipPercentDbl];
    totalWithTipDbl = (totalDbl+tipDbl);
    totalWithTip.text = [NSString stringWithFormat:@"%.2f", totalWithTipDbl];
}
这很好用。我现在想做的(我很难弄清楚)是在文本字段更改时如何更改值。i、 e.有人手动输入自己的小费,如何更新滑块和小费百分比;或者,如果有人手动输入小费百分比,如何更新滑块和小费值


做这件事最好的方法是什么?

这相当容易。我不知道您使用的所有变量,所以在尝试代码时请替换您自己的变量。当键盘退出时,调用以下方法:

- (void)updateTheSliderAndTipPercentage; {
  float percentage = tipTheyEntered / totalCost;
  [slider setValue:percentage animated:YES];
  percentage *= 100; // This is if you want the percentage in [0,100].
  tipPercent.text = [NSString stringWithFormat:@"%f", percentage];
}
编辑:要知道何时调用此方法,只需检查:

- (BOOL)textFieldShouldReturn:(UITextField *)textField; {
  if(textField == tipTheyEnteredTextField){
    [self updateTheSliderAndTipPercentage];
  }
}

希望有帮助

唯一的问题是我们如何知道他们是改变了小费百分比还是小费本身?我需要看更多的代码才能完全回答,但基本上,你需要检查哪个文本字段辞职。我将编辑上面的代码。现在就试用。到目前为止,我真的很感谢你的帮助。