iOS开发-DatePicker控件

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

页面布局

Main.storyboard布局:

iOS开发-DatePicker控件Y

效果:

iOS开发-DatePicker控件

代码实现

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

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

最终的效果如下:

iOS开发-DatePicker控件

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

iOS开发-DatePicker控件

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4225398.html,如需转载请自行联系原作者