UIDatePicker依然继承自UIControl是经常用到的一个时间控件,方便我们用户输入日期.
让控制器监听值发生改变
#import @interface cooljuneViewController :UIViewController <UIPickerViewDataSource,UIPickerViewDelegate> - (IBAction)dateChange:(UIDatePicker *)sender; @property (weak, nonatomic) IBOutletUILabel *dateLable; @property (weak, nonatomic) IBOutletUITextField *nameFiled; @end |
- (IBAction)dateChange:(UIDatePicker *)sender { NSDate *date= sender.date; NSDateFormatter *format=[[NSDateFormatteralloc]init]; format.dateFormat=@"yyyy-MM-dd hh:mm:ss"; //时间转字符串输出 self.dateLable.text=[formatstringFromDate:date]; UIDatePicker *picker=[[UIDatePickeralloc]init]; //设置显示格式 picker.datePickerMode=UIDatePickerModeDate; //设置地区 picker.locale=[[NSLocalealloc]initWithLocaleIdentifier:@"zh_cn"]; //设置默认显示时间 picker.date } |
#import @interface cooljuneViewController :UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> @end |
#import "cooljuneViewController.h" @interface cooljuneViewController () @property(nonatomic,strong)NSArray *firstCol; @property(nonatomic,strong)NSArray *secondCol; @end @implementation cooljuneViewController - (void)viewDidLoad { [superviewDidLoad]; self.firstCol=@[@"test",@"yte",@"fsd",@"55"]; self.secondCol=@[@"hh",@"ff",@"gg",@"kk"]; } #pragma mark - 数据源方法 //返回第component列的行数 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return component==0?self.firstCol.count:self.secondCol.count; } #pragma mark 返回列数 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return2; } //第component列内容 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==0){ returnself.firstCol[row]; }else{ returnself.secondCol[row]; } } //选中某一行发生改变 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if(component==0){
NSString *left=self.firstCol[row];
//根据传入列数返回显示的行数 int rightRow=[pickerViewselectedRowInComponent:1];
NSString *right=self.secondCol[rightRow];
NSLog(@"first=%@,secoden-%@",left,right); }else{ NSString *right=self.secondCol[row]; int leftRow=[pickerViewselectedRowInComponent:0]; NSString *left=self.firstCol[leftRow]; NSLog(@"%@-%@",left,right); } } -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UIView *flagView=[[UIViewalloc]init]; CGFloat viewHeight=40; CGFloat viewWidth=200; flagView.bounds=CGRectMake(0,0, viewWidth, viewHeight);
NSDictionary *dict=self.flags[row]; //国家名称 UILabel *label=[[UILabelalloc]init]; CGFloat lableWith=120; label.frame=CGRectMake(0,0, lableWith, viewHeight); label.text=dict[@"name"]; label.textAlignment=NSTextAlignmentCenter; label.backgroundColor=[UIColorclearColor]; [flagViewaddSubview:label]; //图片 UIImageView *imageView=[[UIImageViewalloc]init]; imageView.frame=CGRectMake(lableWith,0, viewWidth-lableWith, viewHeight);
imageView.image=[UIImageimageNamed:dict[@"icon"]]; [flagViewaddSubview:imageView]; return flagView; } //修改每一行的高度 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return40; } |
总结常用方法(实现自协议里面的方法):
UIPickerViewDataSource协议
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 返回总列数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 返回第component列的行数
UIPickerViewDelegate协议
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; 修改列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component; 修改行高
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
返回第component列第row行显示的字符串内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
返回第component列第row行显示的view内容
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
获取选中行的值