Ios 从2列UIPickerView检索数据
我实现了一个包含2列的UIPickerView,我希望检索第一列值,并将其设置为双精度类型的minutesDuration值,然后从第二列检索另一个名为secondsDuration的双精度值。然后将分钟转换为秒,并将这两个值相加为一个名为totalDuration的双值。 以下是我的didSelectRow方法:Ios 从2列UIPickerView检索数据,ios,objective-c,uipickerview,Ios,Objective C,Uipickerview,我实现了一个包含2列的UIPickerView,我希望检索第一列值,并将其设置为双精度类型的minutesDuration值,然后从第二列检索另一个名为secondsDuration的双精度值。然后将分钟转换为秒,并将这两个值相加为一个名为totalDuration的双值。 以下是我的didSelectRow方法: - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponen
- (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);
}
}
当我运行应用程序时,会出现2个主应用程序中的1个:
-这些值不会被添加
-即使第二列的值没有更改,也会添加这两个值
顺便说一句,总持续时间在头文件中声明。备选方案: 当日期选择器模式设置为
UIDatePickerModeCountDownTimer
时,可以在UIDatePicker
中使用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] );
}
相反,它使用带有
UIDatePickerModeCountDownTimer
模式的UIDatepicker…你能给我发一个教程链接吗?