通过UIPickerView选择器做的一个类似于密码锁的日期时间表

iOS:UIPickerView选择器的使用-LMLPHP

源码如下:

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong,nonatomic) NSArray *weeks;//显示第一列的数据
@property (strong,nonatomic) NSArray *time; //显示第二列的数据 @property (strong,nonatomic)NSArray *years; //显示第三列的显示年
@property (strong,nonatomic)NSArray *months; //显示第四列的显示月
@property (strong,nonatomic)NSArray *days; //显示第五列的显示日 @property (assign,nonatomic)NSInteger number;
@property (strong,nonatomic)NSString *str;
@end
 #import "ViewController.h"

 @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView; @end @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]; //初始化 self.weeks = [NSArray arrayWithObjects:@"Mon",@"Tus",@"Thi",@"Wen",@"Fir",@"Sat",@"Sun", nil]; self.time = [NSArray arrayWithObjects:@"上午",@"中午",@"下午", nil]; self.years = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil]; self.months = [NSArray arrayWithObjects:@"",@"", @"", @"", @"", @"", @"", @"", @"", @"", @"", @"", nil]; self.days = [NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil]; //设置pickerView的DataSource和Delegate
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark-pickerView的代理方法 //设置有多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
} //设置每列多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component)
{
case :
self.number = self.weeks.count;
break;
case :
self.number = self.time.count;
break;
case :
self.number = self.years.count;
break;
case :
self.number = self.months.count;
break;
case :
self.number = self.days.count;
break;
} return self.number; } //设置每行每列显示的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{ switch (component)
{
case :
self.str = self.weeks[row];
break;
case :
self.str = self.time[row];
break;
case :
self.str = self.years[row];
break;
case :
self.str = self.months[row];
break;
case :
self.str = self.days[row];
break;
} return self.str; } //通过代理方法didSelectRows获取数据
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//NSLog(@"列:%ld,行:%ld",component,row);
switch (component)
{
case :
NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[row],self.time[[pickerView selectedRowInComponent:]],self.years[[pickerView selectedRowInComponent:]],self.months[[pickerView selectedRowInComponent:]],self.days[[pickerView selectedRowInComponent:]]]);
break;
case :
NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:]],self.time[row],self.years[[pickerView selectedRowInComponent:]],self.months[[pickerView selectedRowInComponent:]],self.days[[pickerView selectedRowInComponent:]]]);
break;
case :
NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:]],self.time[[pickerView selectedRowInComponent:]],self.years[row],self.months[[pickerView selectedRowInComponent:]],self.days[[pickerView selectedRowInComponent:]]]);
break;
case :
NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:]],self.time[[pickerView selectedRowInComponent:]],self.years[[pickerView selectedRowInComponent:]],self.months[row],self.days[[pickerView selectedRowInComponent:]]]);
break;
case :
NSLog(@"%@",[NSString stringWithFormat:@"%@,%@,%@,%@,%@",self.weeks[[pickerView selectedRowInComponent:]],self.time[[pickerView selectedRowInComponent:]],self.years[[pickerView selectedRowInComponent:]],self.months[[pickerView selectedRowInComponent:]],self.days[row]]);
break;
}
}
@end
05-08 15:29