根据另一个字段的值设置文本字段的值
Posted
技术标签:
【中文标题】根据另一个字段的值设置文本字段的值【英文标题】:Setting the value of a text field dependent on the value of another field 【发布时间】:2011-12-20 20:12:14 【问题描述】:我有两个UITextField
s。第一个文本字段称为personBMI
并表示一个数字。第二个是gaugeWeight
。我希望我的 gaugeWeight
字段根据 personBMI
UITextField
是小于一个值、介于两个值之间(例如,20-24)还是大于一个值来表达一个词。
我知道这个if
/then
声明应该表达为我的IBAction
的一部分。我如何为我的gaugeWeight
字段编码来表达定义personBMI
字段结果的单词,基于它小于、介于或大于一个值?
【问题讨论】:
到目前为止你有没有尝试过? 【参考方案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/ ([personHeight.text floatValue] *[personHeight.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
希望这会有所帮助。
【讨论】:
以上是关于根据另一个字段的值设置文本字段的值的主要内容,如果未能解决你的问题,请参考以下文章