我有一个转换器应用程序,我希望它在我将数字(应该转换的)输入到文本字段时更新标签。我希望它自动更新,所以我不需要再次选择轮子上的单元来更新它。
这是我的代码:
- (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];
}
}
我应该在这里做点什么吗?
- (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;
}