设置文本字段的值取决于另一个字段的值

时间:2011-12-20 20:12:15

标签: objective-c ios cocoa-touch uitextfield

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

我知道if / then语句应该表示为IBAction的一部分。我如何编码我的gaugeWeight字段来表达定义personBMI字段结果的字词,因为它小于,介于或大于某个值?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

#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

希望这有帮助。