您的位置:首页 > 移动开发 > IOS开发

iOS开发-DatePicker控件

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

页面布局

Main.storyboard布局:


Y

效果:



代码实现

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

- (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:nil, nil];
[alterView show];
}


最终的效果如下:



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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: