我正在制作一个定时器应用程序,需要向用户显示小时,分钟和秒。我尝试使用UIDatePicker,但它只显示几小时和分钟作为选择。不是秒在线上进行一些研究后,我发现在UIDatePicker中没有办法获得秒数,我必须从头开始编写自己的UIPickerView。
所以我的问题是,有没有示例代码,有人写了一个CustomUIPickerView几个小时,最小和秒,我可以纳入我的项目? UIDatePicker有一个很好的叠加时间&在用户旋转拨盘时,打开保留的文本。如果有人在他们的自定义选择器中也添加了这将是很好的。我不喜欢从头开始编写一个自定义的UIPickerView。谢谢。
解决方法
Alrighty的人,这里是你的UIPickerView获得几小时/分钟/秒的代码。您可以添加3个标签,策略性地将它们放置在选择器上。我也附上了一张照片。
如果你喜欢这个答案,做点评吧!良好的开发者分享知识,不像某些人所暗示的那样隐藏。有乐趣的编码!
@interface v1AddTableViewController : UITableViewController { IBOutlet UIPickerView *pickerView; NSMutableArray *hoursArray; NSMutableArray *minsArray; NSMutableArray *secsArray; NSTimeInterval interval; } @property(retain,nonatomic) UIPickerView *pickerView; @property(retain,nonatomic) NSMutableArray *hoursArray; @property(retain,nonatomic) NSMutableArray *minsArray; @property(retain,nonatomic) NSMutableArray *secsArray;
在你的.m文件中放这个
@synthesize pickerView; @synthesize hoursArray; @synthesize minsArray; @synthesize secsArray; @synthesize interval; - (void)viewDidLoad { [super viewDidLoad]; //initialize arrays hoursArray = [[NSMutableArray alloc] init]; minsArray = [[NSMutableArray alloc] init]; secsArray = [[NSMutableArray alloc] init]; Nsstring *strVal = [[Nsstring alloc] init]; for(int i=0; i<61; i++) { strVal = [Nsstring stringWithFormat:@"%d",i]; //NSLog(@"strVal: %@",strVal); //Create array with 0-12 hours if (i < 13) { [hoursArray addobject:strVal]; } //create arrays with 0-60 secs/mins [minsArray addobject:strVal]; [secsArray addobject:strVal]; } NSLog(@"[hoursArray count]: %d",[hoursArray count]); NSLog(@"[minsArray count]: %d",[minsArray count]); NSLog(@"[secsArray count]: %d",[secsArray count]); } //Method to define how many columns/dials to show - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } // Method to define the numberOfRows in a component using the array. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component { if (component==0) { return [hoursArray count]; } else if (component==1) { return [minsArray count]; } else { return [secsArray count]; } } // Method to show the title of row for a component. - (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { switch (component) { case 0: return [hoursArray objectAtIndex:row]; break; case 1: return [minsArray objectAtIndex:row]; break; case 2: return [secsArray objectAtIndex:row]; break; } return nil; } -(IBAction)calculateTimeFromPicker { Nsstring *houRSStr = [Nsstring stringWithFormat:@"%@",[hoursArray objectAtIndex:[pickerView selectedRowInComponent:0]]]; Nsstring *minsstr = [Nsstring stringWithFormat:@"%@",[minsArray objectAtIndex:[pickerView selectedRowInComponent:1]]]; Nsstring *secsstr = [Nsstring stringWithFormat:@"%@",[secsArray objectAtIndex:[pickerView selectedRowInComponent:2]]]; int hoursInt = [houRSStr intValue]; int minsInt = [minsstr intValue]; int secsInt = [secsstr intValue]; interval = secsInt + (minsInt*60) + (hoursInt*3600); NSLog(@"hours: %d ... mins: %d .... sec: %d .... interval: %f",hoursInt,minsInt,secsInt,interval); Nsstring *totalTimeStr = [Nsstring stringWithFormat:@"%f",interval]; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。