IBAction float,根据输入具有相同的不同数字

发布于 2024-12-27 11:11:20 字数 448 浏览 1 评论 0原文

刚接触 Objective-C,我在任何地方都找不到这个问题的答案。我正在运行 IBAction 进行计算,但我需要标签的输入实际上等于方程中的其他内容。

例如:

-(IBAction)calculate; {
  float a = 1.05 if ([label1.text] == 1in);
        a = 2.07 if ([label1.text] == 2in);
        a = 3.07 if ([label1.text] == 3in);
  float b = a*([textField1.text floatValue]);
        label2.text = [[NSString alloc] initWithFormat:@"%2.f", b];
  }

我知道我还没有达到正确的目的,但我希望你能明白我在寻找什么。

谢谢你!

New to objective-c and I just can't find the answer to this question anywhere. I'm running a IBAction for a calculation but I need the input from a label to actually equal something else in the equation.

For example:

-(IBAction)calculate; {
  float a = 1.05 if ([label1.text] == 1in);
        a = 2.07 if ([label1.text] == 2in);
        a = 3.07 if ([label1.text] == 3in);
  float b = a*([textField1.text floatValue]);
        label2.text = [[NSString alloc] initWithFormat:@"%2.f", b];
  }

I know I'm not even close to getting it right but I hope you get the idea as to what I'm looking for.

Thank you!

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

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

发布评论

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

评论(1

静若繁花 2025-01-03 11:11:21
- (void)calculate {
  float a;
  if ([self.inputField.text isEqualToString:@"1in"])      a = 1.05f;
  else if ([self.inputField.text isEqualToString:@"2in"]) a = 2.07f;
  else if ([self.inputField.text isEqualToString:@"3in"]) a = 3.07f;
  else a = 0.0f;

  if (a) {
    float b = a * ([self.inputField.text floatValue]);
    self.inputField.text = [[NSString alloc] initWithFormat:@"%.2f", b];
  }
  else self.inputField.text = @"Invalid";
}

- (void)viewDidLoad
{
  //...

  self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 300.0f, 30.0f)];
  [self.inputField setBackgroundColor:[UIColor grayColor]];
  [self.view addSubview:self.inputField];

  UIButton * doneButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300.0f, 30.0f)];
  [doneButton setBackgroundColor:[UIColor blackColor]];
  [doneButton setTitle:@"Done" forState:UIControlStateNormal];
  [doneButton addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:doneButton];
  [doneButton release];
}

注意:我将输出格式 %2.f 替换为 %.2f,因为我猜您可能需要这种格式。例如:

Input > 1in
Output< 1.05

Input > 2in
Output< 4.14

Input > 3in
Output< 9.21
- (void)calculate {
  float a;
  if ([self.inputField.text isEqualToString:@"1in"])      a = 1.05f;
  else if ([self.inputField.text isEqualToString:@"2in"]) a = 2.07f;
  else if ([self.inputField.text isEqualToString:@"3in"]) a = 3.07f;
  else a = 0.0f;

  if (a) {
    float b = a * ([self.inputField.text floatValue]);
    self.inputField.text = [[NSString alloc] initWithFormat:@"%.2f", b];
  }
  else self.inputField.text = @"Invalid";
}

- (void)viewDidLoad
{
  //...

  self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 300.0f, 30.0f)];
  [self.inputField setBackgroundColor:[UIColor grayColor]];
  [self.view addSubview:self.inputField];

  UIButton * doneButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 55.0f, 300.0f, 30.0f)];
  [doneButton setBackgroundColor:[UIColor blackColor]];
  [doneButton setTitle:@"Done" forState:UIControlStateNormal];
  [doneButton addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:doneButton];
  [doneButton release];
}

Note: I replaced the output format %2.f to %.2f, cause I guess you may need this format. E.g:

Input > 1in
Output< 1.05

Input > 2in
Output< 4.14

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