我有一个转换器应用程序,我希望它在我输入数字(应该转换)到文本字段时更新标签。我想让它自动更新,所以我不需要再次选择轮子上的单位进行更新。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_convertFrom = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @133.4246575f, @3.780821918f,
@142465.7534f, @23780.8f];
_convertTo = @[@"MTPA", @"MMcf/day",
@"Mill.Sm3/day", @"MMBTU", @"Boe/day"];
_convertRates = @[ @1.0f, @133.4246575f, @3.780821918f,
@142465.7534f, @23780.8f];
}
}
shuold我在这里做点什么?
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTouched:(id)sender
{
[inputText resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (component == 0) {
return [_convertFrom count];
}
return [_convertTo count];
}
- (NSString *) pickerView: (UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom objectAtIndex:row];
}
return [_convertTo objectAtIndex:row];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float relative = to / from;
float result = relative * convertValue;
NSString *convertFromName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *resultString = [[NSString alloc]initWithFormat:
@" %.4f %@ = %.4f %@",convertValue, convertFromName, result, convertToName];
resultLabel.text = resultString;
}
答案 0 :(得分:3)
您的问题与选择器视图无关。您希望监听文本字段的更改,并根据输入到文本字段中的最新文本更新转换和标签。
因此,此时您的选择器视图委托方法和文本字段更改方法都会调用从选择器获取转换的方法和文本字段中的文本,然后执行转换,最后更新标签。
编辑:更多详情
重构当前的选择器视图委托方法,如下所示:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self updateConversionLabel];
}
- (void)updateConversionLabel {
float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float relative = to / from;
float result = relative * convertValue;
NSString *convertFromName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *resultString = [[NSString alloc]initWithFormat:
@" %.4f %@ = %.4f %@",convertValue, convertFromName, result, convertToName];
resultLabel.text = resultString;
}
然后,您需要创建一个方法来处理对文本字段的更改:
- (void)textFieldChanged:(UITextField *)textField {
[self updateConversionLabel];
}
现在在IB中,将textFieldChanged:
方法连接到inputText
文本字段的“值已更改”事件。我不使用IB,所以我不知道这样做的确切术语。