从2列UIPickerView中检索数据

时间:2013-09-18 13:43:30

标签: ios objective-c uipickerview

我实现了一个带有2列的UIPickerView,我想检索第一个列值并设置为double类型的minutesDuration值,并从第二列中检索另一个名为secondsDuration的double值。然后将分钟转换为秒,并将这两个值一起添加到名为totalDuration的double值中。 这是我的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);
    }
}

当我运行app 1 of 2 main时出现: - 不添加值 - 即使第二列值未更改,也会添加两个值 顺便说一下总持续时间是在头文件中声明的。

1 个答案:

答案 0 :(得分:0)

<强>替代:

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

<强>·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] );


  }