UISlider 帮助

发布于 2024-11-01 22:39:21 字数 1268 浏览 6 评论 0原文

在我的 UISlider 中,如果我的值“tipPercentage”达到 10 或更高,标签“costWithTipLabel”将设置回文本字段的“costWithoutTip”值,小费增加 10%。

如果您能查看我的代码并让我知道导致此问题的问题,我将不胜感激。

提前致谢。

- (IBAction)aSliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    if (slider == tipslide) {
        NSString *tip = [NSString stringWithFormat:@"%.f", slider.value * 100];
        float tipPercentage = [tip floatValue];
        NSString *multiplier = [NSString stringWithFormat:@"1.%.f", tipPercentage];
        [costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithoutTip text] floatValue] * [multiplier floatValue]]];
        [tipTextLabel setText:[[NSString stringWithFormat:@"Tip (%.f", slider.value *100]stringByAppendingString:@"%):"]];
    }
    else if (slider == peopleslide) {
        NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];
        float numOfPeople = [p floatValue];
        [numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each (%.f):", numOfPeople]];
        [numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numOfPeople]];
    }
    [totalBillCost setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]]];
}

With my UISlider, if my value 'tipPercentage' gets to 10 or more, the label 'costWithTipLabel' gets set back to the textField's 'costWithoutTip' value starting with a 10% tip increase.

I would really appreciate it if you could take a look at my code and let me know of the problem causing this.

Thanks in advanced.

- (IBAction)aSliderChanged:(id)sender {
    UISlider *slider = (UISlider *)sender;
    if (slider == tipslide) {
        NSString *tip = [NSString stringWithFormat:@"%.f", slider.value * 100];
        float tipPercentage = [tip floatValue];
        NSString *multiplier = [NSString stringWithFormat:@"1.%.f", tipPercentage];
        [costWithTipLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithoutTip text] floatValue] * [multiplier floatValue]]];
        [tipTextLabel setText:[[NSString stringWithFormat:@"Tip (%.f", slider.value *100]stringByAppendingString:@"%):"]];
    }
    else if (slider == peopleslide) {
        NSString *p = [NSString stringWithFormat:@"%.f", slider.value*10];
        float numOfPeople = [p floatValue];
        [numberOfPeopleTextLabel setText:[NSString stringWithFormat:@"Each (%.f):", numOfPeople]];
        [numberOfPeopleLabel setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]/numOfPeople]];
    }
    [totalBillCost setText:[NSString stringWithFormat:@"%.2f", [[costWithTipLabel text] floatValue]]];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-11-08 22:39:21

我认为这与字符串的意外格式有关。我建议您将 stringWithFormat 分配分解为多个简单(非嵌套)语句,然后尽可能使用通用格式 (%f) NSLog 您的值。然后您应该能够找出问题所在。

I expect it has to do with unexpected formatting of your strings. I suggest you break your stringWithFormat assignments into multiple simple (non-nested) statements, and then NSLog your values using as generic a format (%f) as possible. Then you should be able to track down where the problem is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文