iOS datepicker 怎么用代码设置只显示年月日
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式。
您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Timer四种模式。
具体的步骤如下:
1.新建一个Singe View Application工程 ,拖动一个DatePicker控件 和 Button到界面。
2.然后关联Button事件(可以按住Ctrl拖动实现)
3.实现代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*typedef enum {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
} UIDatePickerMode;
*/
//datePicker.datePickerMode = UIDatePickerModeDateAndTime;//这儿即是改变显示的模式
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)datePick:(id)sender {
NSDate *select = [datePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
NSString *dateAndTime = [dateFormatter stringFromDate:select];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" 时间提示" message: dateAndTime delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}