我实现了一个具有 2 列的 UIPickerView,我想检索第一列值并将其设置为 double 类型的 minutesDuration 值,并从第二列中检索另一个名为 secondsDuration 的 double 值。然后将分钟转换为秒,并将两个值相加成一个名为 totalDuration 的双精度值。这是我的 didSelectRow 方法:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *minute =[[NSString alloc] init];
NSString *seconds = [[NSString alloc] init];
//Here, like the table view you can get the each section of each row if you've multiple sections
if(component == 0){
int i =row;
int j =row;
minute = [NSString stringWithFormat:@"%@", [minutesArray objectAtIndex:i]];
seconds = [NSString stringWithFormat:@"%@", [secondsArray objectAtIndex:j]];
double minutesDuration = [minute doubleValue] * 60;
NSLog(@"%f", minutesDuration);
double secondsDuration = [seconds doubleValue];
totalDuration = minutesDuration + secondsDuration;
NSLog(@"%f", totalDuration);
}
else if (component == 1){
int i =0;
int j =row;
minute = [NSString stringWithFormat:@"%@", [minutesArray objectAtIndex:i]];
seconds = [NSString stringWithFormat:@"%@", [secondsArray objectAtIndex:j]];
double minutesDuration = [minute doubleValue] * 60;
NSLog(@"%f", minutesDuration);
double secondsDuration = [seconds doubleValue];
NSLog(@"%f", secondsDuration);
totalDuration = minutesDuration + secondsDuration;
NSLog(@"%f", totalDuration);
}
}
当我运行应用程序 1 of 2 时,主要出现: - 未添加值 - 即使第二列值未更改,也会添加两个值顺便说一句,总持续时间在头文件中声明。