birthDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,500/2,self.view.frame.size.width, 88/2)]; birthDate.datePickerMode = UIDatePickerModeDate;// 设置只显示日期 [birthDate addTarget:self action:@selector(setBirthDate:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:birthDate]; //设置toolbar pickerToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 470/2, self.view.frame.size.width, 30)]; [pickerToolbar setBackgroundColor:[UIColor clearColor]]; UIBarButtonItem* sureItem=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(hiddenPicker:)]; sureItem.tag = 10; UIBarButtonItem* cancelItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(hiddenPicker:)]; UIBarButtonItem* flexItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; flexItem.tag = 11; // pickerToolbar.hidden=YES; pickerToolbar.items=@[sureItem,flexItem,cancelItem]; [self.view addSubview:pickerToolbar]; /* *@brief 选择出生日期方法 ** */ - (void)setBirthDate:(id)sender { NSLog(@"设置出生日期"); // 获取用户通过UIDatePicker设置的日期和时间 NSDate *selected = [birthDate date]; // 创建一个日期格式器 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // 为日期格式器设置格式字符串 [dateFormatter setDateFormat:@"MM月dd日"]; // 使用日期格式器格式化日期、时间 destDateString = [dateFormatter stringFromDate:selected]; NSLog(@"string === %@",destDateString); [personMessageArr replaceObjectAtIndex:5 withObject:destDateString]; NSLog(@"%@",personMessageArr); /// 调用修改日期接口 }