我的应用是带有多组分拾取轮的转换器。它用于转换一些变量。您可以看到的数字仅用于测试,没有什么重要的。
- (void)viewDidLoad
{
[super viewDidLoad];
_convertFrom = @[@"One (1)", @"Two (2)",
@"Three (3)", @"Four (4)", @"Five (5)"];
_convertRates = @[ @1.0f, @2.0f, @3.0f,
@4.0f, @5.0f];
_convertTo = @[@"One (1)", @"Two (2)",
@"Three (3)", @"Four (4)", @"Five (5)"];
_convertRates = @[ @1.0f, @2.0f, @3.0f,
@4.0f, @5.0f];
}
我在iOS 6模拟器中测试应用时显示的数字有一个小问题。正如你在这里看到的那样:
当我尝试下列表时,数字会发生变化。但它们的价值却没有。 如果有人有时间检查出来并且可能找到解决方案,那将非常有用:)
这是在这种情况下可能很重要的其余代码:
#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:[pickerView selectedRowInComponent:0]];
}
return [_convertTo objectAtIndex:[pickerView selectedRowInComponent:1]];
}
#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 = [dollarText.text floatValue];
float to = convertTo;
float from = convertFrom;
float dollars = input;
float result = to * from * dollars;
float mtpaTilMtpaFloat = dollars * 1;
NSString *resultString = [[NSString alloc] initWithFormat:
@"result = %.2f * %.2f = %.2f", from, to, result];
resultLabel.text = resultString;
NSString *mtpaTilmtpa = [[NSString alloc ] initWithFormat:
@" %.2f MTPA = %.2f MTPA", dollars, mtpaTilMtpaFloat];
if (from == 1, to == 1) { resultLabel.text = mtpaTilmtpa;
}
当然,告诉我是否需要显示更多代码以便您能够理解它。这是我第一次使用这个网站btw :)
答案 0 :(得分:3)
不要使用selectedRowInComponent
。您不想显示在所有行中选择的值。只需使用row
- (NSString *) pickerView: (UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom objectAtIndex:row];
}
return [_convertTo objectAtIndex:row];
}