iOS开发-DatePicker控件

十度 IOS 2015年12月01日 收藏

时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time  , Count Down Timer,Demo比较简单,需要一个DatePicker控件和一个Button显示时间或者日期,具体实现如下:

页面布局

Main.storyboard布局:

Y

效果:

代码实现

首先将控件拖入ViewController.h中,将Button的点击拖入ViewController.m中:

  1. - (IBAction)showDate:(id)sender {
  2. //日期选择时候的时间
  3. NSDate *currentDate = [_currentDatePicker date];
  4. NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
  5. //显示的日期格式
  6. // [myFormatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
  7. //2015
  8. [myFormatter setDateFormat:@"yyyy年MM月dd日"];
  9. NSString *time = [myFormatter stringFromDate:currentDate];
  10. UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"当前时间" message:time delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  11. [alterView show];
  12. }

最终的效果如下:

DatePicker的Mode我选择的是Time模式,大家可以根据需要自己选择,默认的时显示英文的,如果DatePicker需要切换中英文那么可以选择Locale选择中文: