0

我实现了一个具有 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 时,主要出现: - 未添加值 - 即使第二列值未更改,也会添加两个值顺便说一句,总持续时间在头文件中声明。

4

1 回答 1

0

选择:

UIDatePicker当日期选择器模式设置为时,您可以使用 countDownDuration 属性UIDatePickerModeCountDownTimer。倒计时持续时间以秒为单位。

。H

@interface ThisPicker : UIViewController {
IBOutlet UIDatePicker *datePicker;
}
-(IBAction)buttonPressed:(id)sender;
@end

.m

viewdidLoad
{
   datePicker=[[UIDatePicker alloc]init];
    dp.datePickerMode=UIDatePickerModeCountDownTimer;

    [self.view addSubview:dp];

}
-(IBAction)buttonPressed:(id)sender
  {

   NSLog(@"time in seconds: %@",[NSString stringWithFormat:@"%f",datePicker.countDownDuration] );


  }
于 2013-09-18T14:05:17.720 回答