1.首先要遵守
UIPickerViewDataSource, UIPickerViewDelegate 这两个协议
2.所需要的对象
@property (nonatomic ,strong) NSMutableArray *year_Array; @property (nonatomic ,strong) NSArray *according_moth_Array; @property (nonatomic ,strong) NSArray *actual_starting_moth_Array; //@property (nonatomic ,strong) NSArray *actual_termination_moth_Array; @property (nonatomic ,strong) NSString *current_year_string; @property (nonatomic ,strong) NSString *current_moth_string; @property (nonatomic, strong) UIPickerView *datePicker;
3.设置时间和记录当前时间
- (void)createDate { NSDate *current_time = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; self.current_year_string = [NSString stringWithFormat:@"%@年",[formatter stringFromDate:current_time]]; [formatter setDateFormat:@"MM"]; self.current_moth_string = [NSString stringWithFormat:@"%@月",[formatter stringFromDate:current_time]]; for (int32_t i = 1970; i<= 2050; i++) { [self.year_Array addObject:[NSString stringWithFormat:@"%d年",i]]; } self.according_moth_Array = @[@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月"]; self.actual_starting_moth_Array =@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"]; // self.actual_termination_moth_Array = @[@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"01"]; }
4.初始化 UIPickerView
self.datePicker = [[UIPickerView alloc] init]; self.datePicker.frame = CGRectMake(0, 0, ScreenWidth - 80, 162); self.datePicker.delegate = self; self.datePicker.dataSource = self; [self.datePicker selectRow:[self.year_Array indexOfObject:self.current_year_string] inComponent:0 animated:YES]; [self.datePicker selectRow:[self.according_moth_Array indexOfObject:self.current_moth_string] inComponent:1 animated:YES];
5.实现
UIPickerViewDatasource
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *pickerLabel = (UILabel *)view; if (pickerLabel == nil) { CGRect frame = CGRectMake(0.0, 0.0, 100, 60); pickerLabel = [[UILabel alloc] initWithFrame:frame]; [pickerLabel setTextAlignment:NSTextAlignmentCenter]; [pickerLabel setBackgroundColor:[UIColor clearColor]]; [pickerLabel setFont:[UIFont systemFontOfSize:22.0f]]; } if (component == 0) { pickerLabel.text = [self.year_Array objectAtIndex:row]; } else { pickerLabel.text = [self.according_moth_Array objectAtIndex:row]; } return pickerLabel; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (component == 0) { return 100; } else { return 100; } } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return [self.year_Array count]; } else { return [self.according_moth_Array count]; } }
6. 获取显示的时间
self.beginTimeTF.text = [NSString stringWithFormat:@"%d-%@",[[self.year_Array objectAtIndex:[self.datePicker selectedRowInComponent:0]] intValue],[self.actual_starting_moth_Array objectAtIndex:[self.datePicker selectedRowInComponent:1]]];
原文:http://www.cnblogs.com/xiaoxiaolei/p/4964326.html