IOS - DatePicker的使用

时间:2024-08-26 12:35:02

UIDatePicker *picker = [[UIDatePicker alloc] init];

picker.datePickerMode = UIDatePickerModeDate;

picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

// 设置键盘

self.myfield.inputView = picker;

// 设置键盘上面的工具条

self.myfield.inputAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

// 设置键盘上的ToolBar

UIBarButtonItem *prevButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"上一项" style:UIBarButtonItemStyleBordered target:selfaction:@selector(ShowPrevious)];

UIBarButtonItem *nextButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一项" style:UIBarButtonItemStyleBordered target:selfaction:@selector(ShowNext)];

UIBarButtonItem *hiddenButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleBordered target:selfaction:@selector(HiddenKeyBoard)];

UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIToolbar *accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

accessoryView.barStyle = UIBarStyleBlackTranslucent;

accessoryView.items = [NSArray arrayWithObjects:prevButtonItem,nextButtonItem,spaceButtonItem,hiddenButtonItem,nil];

self.myfield.inputAccessoryView = accessoryView;