【问题标题】:Setting the value of a text field dependent on the value of another field根据另一个字段的值设置文本字段的值
【发布时间】:2011-12-20 20:12:14
【问题描述】:

我有两个UITextFields。第一个文本字段称为personBMI 并表示一个数字。第二个是gaugeWeight。我希望我的 gaugeWeight 字段根据 personBMI UITextField 是小于一个值、介于两个值之间(例如,20-24)还是大于一个值来表达一个词。

我知道这个if/then 声明应该表达为我的IBAction 的一部分。我如何为我的gaugeWeight 字段编码来表达定义personBMI 字段结果的单词,基于它小于、介于或大于一个值?

【问题讨论】:

  • 到目前为止你有没有尝试过?

标签: objective-c ios cocoa-touch uitextfield


【解决方案1】:

这样的事情将是一个开始......

CGFloat bmi = [self.personBMI intValue];
NSString *classification = @"Unclassified";

if (bmi <= 16.0f) {
    classification = @"Severely underweight";
} else if (bmi <= 18.5f) {
    classification = @"Underweight";
} // the rest

self.gaugeWeight.text = classification;

【讨论】:

  • 我的 IBAction 如下:-(IBAction)calculate:(id)sender { float floatPersonBMI=[personWeight.text floatValue]*703/ ([personH​​eight.text floatValue] *[personH​​eight.text floatValue ]); NSString *stringPersonBMI=[[NSString alloc]initWithFormat:@"%f",floatPersonBMI]; personBMI.text=stringPersonBMI;.....所以我想基于该 IBAction 制作一个“if,then”,以在另一个 UITextField 中返回文本,类似于您上面提到的“严重体重不足”和“体重不足”
【解决方案2】:
#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *personBMI;
@property (weak, nonatomic) IBOutlet UITextField *gaugeWeight;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.personBMI.delegate = self;
    [self.personBMI addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventValueChanged];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textfieldDIdChange{
    if ([self.personBMI.text floatValue]< 20 && [self.personBMI.text floatValue] > 24) {
        self.gaugeWeight.text = @"Put some text here";
    }
}

@end

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2020-08-04
    相关资源
    最近更新 更多