UIDatePicker继承关系如下:

  UIDatePicker-->UIControl-->UIView-->UIResponder-->NSObject


1、创建UIDatePicker

创建一个UIDatePicker控件并显示出来来看看这玩意长什么模样,代码:

     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
[self.view addSubview:datePicker];//添加到当前的视图中显示出来

样子如下:

2、配置UIDatePicker

2.1 UIDatePicker国际化

我们看见默认的UIDatePicker是英文的,这让我很不爽,用下面这个方法让它变成中文

 @property(nonatomic, retain) NSLocale *locale

下面赶紧用这个方法给UIDatePicker对象设置上让它变成中文,英文看着实在不爽:

     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
NSLocale *chineseLocale = [NSLocale localeWithLocaleIdentifier:@"zh_cn"]; //创建一个中文的地区对象
[datePicker setLocale:chineseLocale]; //将这个地区对象给UIDatePicker设置上
[self.view addSubview:datePicker];//添加到当前的视图中显示出来

设置好了,来看看效果吧

恩,现在UIDatePicker变成中文了,但是有个问题,不管这个IOS系统是什么语言,它总是显示成中文,不利于国际化,下面我们修改代码实现UIDatePicker的国际化

     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
NSLocale *currentLocale = [NSLocale currentLocale]; //取得当前IOS用户所设置的地区信息
[datePicker setLocale:currentLocale]; //给UIDatePicker对象设置地区信息
[self.view addSubview:datePicker];//添加到当前的视图中显示出来

使用以上代码后有可能程序显示的效果还是英文的,这需要设置你的IPhone的International信息才可以。

 

如上图我设置的是语言为英文,下面的地区格式设置为中国,日历为公历(吐槽苹果都IOS7了,中国农历还是没实现,连小日本的日历都有,这是什么意思),我的程序显示效果如下:

 2.2 UIDatePicker显示模式

用于配置UIDatePicker显示模式的方法如下

 @property(nonatomic) UIDatePickerMode datePickerMode

该方法用于配置UIDatePicker是只显示日期选择、时间选择、或者既有日期又有时间选择(默认就是这种),还是显示为一个倒计时器。

UIDatePickerMode是个枚举分别对应那四种方法的值。定义如下:

 typedef enum {
UIDatePickerModeTime, //只有时间选择的模式
UIDatePickerModeDate, //只有日期选择的模式
UIDatePickerModeDateAndTime, //既有时间又有日期的选择模式(默认这种)
UIDatePickerModeCountDownTimer //倒计时器模式
} UIDatePickerMode;

由于上面三种模式都是默认模式的变体,比较好理解,下面只实现一下倒计时器,看看啥子模样,

     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
[datePicker setDatePickerMode:UIDatePickerModeCountDownTimer]; //设置显示模式是一个倒计时器
[self.view addSubview:datePicker];//添加到当前的视图中显示出来

显示效果为:

3、UIDatePicker的事件处理

3.1 普通形态的UIDatePicker添加事件

上面的方法用于创建和配置UIDatePicker的显示模式,UIDatePicker也是继承与UIControl的因此也可以使用

 - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

下面来给UIDatePicker添加一个事件,当用户改变了UIDatePicker的值时就弹出一个警告框显示用户选择的日期及时间,下面是初始化以及添加事件的代码:

     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
[datePicker addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventValueChanged]; //为UIDatePicker添加一个事件当UIDatePicker的值被改变时触发
[[self view] addSubview:datePicker];//添加到当前的视图中显示出来

下面是事件处理方法:

 - (void)changeTime:(UIDatePicker*)sender
{
NSDateFormatter * df = [[NSDateFormatter alloc] init]; //初始化一个日期格式化器
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]]; //初始化一个中国的区域信息对象
[df setDateStyle:NSDateFormatterFullStyle]; //设置日期格式化器的日期显示样式为完整样式
[df setTimeStyle:NSDateFormatterFullStyle]; //设置日期格式化器的事件显示样式为完整样式
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIDatePicker事件" message:[df stringFromDate:[sender date]] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];//初始化一个UIAlertView用户显示用户选择的日期及时间
[alert show];//显示这个UIAlertView
}

下面来看效果:

 3.2 倒计时器形态的UIDatePicker添加事件

这里用倒计时器的模式以及计时器对象做一个倒计时程序,下面贴出完整的ViewController代码,首先是头文件:

 #import <UIKit/UIKit.h>

 @interface ZFCViewController : UIViewController

 @property (nonatomic,strong) NSTimer *timer; //计时器对象
@property (nonatomic,strong) UIDatePicker *datePicker; //倒计时选择器对象
@property (nonatomic,assign) double leftSeconds; //倒计时选择器对象剩余多少秒 @end

接着是实现文件:

 //
// ZFCViewController.m
// UIDatePickerTest
//
// Created by 赵 福成 on 14-5-19.
// Copyright (c) 2014年 ZhaoFucheng. All rights reserved.
// #import "ZFCViewController.h" @interface ZFCViewController () @end @implementation ZFCViewController NSString *msg; //用于显示还有多少秒的信息变量
NSDateFormatter *df; //用于格式化日期的显示格式
UIAlertView *alert; //用于循环时弹出的警告框 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)loadView
{
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self setView:rootView];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , )];//创建一个UIDatePicker对象
[self.datePicker setDatePickerMode:UIDatePickerModeCountDownTimer]; //设置为倒计时器
[self.datePicker addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventValueChanged]; //为UIDatePicker添加一个事件当UIDatePicker的值被改变时触发
[[self view] addSubview:self.datePicker];//添加到当前的视图中显示出来
} - (void)changeTime:(UIDatePicker*)sender
{
[self setLeftSeconds:[sender countDownDuration]];//取得这个UIDatePicker倒计时器还剩多少秒
[sender setEnabled:NO];//当触发了一个事件就将UIDatePicker设置为禁用
[self setTimer:[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES]];//设置了一个计时器对象,每隔一秒执行一次
} - (void)countDownTimer
{
self.leftSeconds -= ; //每次执行减去一秒
if (self.leftSeconds <= ) { //如果时间为0了
[self.timer invalidate]; //将取消计时器
[self.datePicker setEnabled:YES]; //将UIDatePicker设置为可用
}
msg = [NSString stringWithFormat:@"还剩%g秒",self.leftSeconds]; //修改UIAlertView上的剩余时间提示信息
[alert setMessage:msg]; //将信息放到UILaterView上
if (!alert.visible) { //这个UIAlertView是否已经显示
[alert show];//如果没有显示就显示这个UIAlertView
}
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
df = [[NSDateFormatter alloc] init]; //初始化一个日期格式化器
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]]; //初始化一个中国的区域信息对象
[df setDateStyle:NSDateFormatterFullStyle]; //设置日期格式化器的日期显示样式为完整样式
[df setTimeStyle:NSDateFormatterFullStyle]; //设置日期格式化器的事件显示样式为完整样式
alert = [[UIAlertView alloc] initWithTitle:@"UIDatePicker事件" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];//初始化一个UIAlertView用户显示用户选择的日期及时间
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end

这样就做好了一个倒计时程序,效果图如下:

先到这里、以后继续添加。。

UIDatePicker控件的更多相关文章

  1. iOs基础篇(二十二)—— UIPickerView、UIDatePicker控件的使用

    一.UIPickerView UIPickerView是一个选择器控件,可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. 1.常用属性 (1)num ...

  2. iOS开发中UIDatePicker控件的使用方法简介

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四 ...

  3. iOS学习之UIDatePicker控件使用

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. ,   您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Ti ...

  4. Swift - 日期选择控件(UIDatePicker)的用法

    1,使用storyboard创建日期选择控件 首先我们将一个UIDatePicker控件和一个按钮直接添加到Main.Storyboard上.该按钮是为了点击时弹出提示框显示当前选择的日期和时间. 同 ...

  5. 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )

    转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...

  6. Swift学习之熟悉控件

    最近是比较清闲一些的,对于一个开发者来说,这也是一个很好的充电机会.以前做项目都是使用Objective-C去开发,但我们都知道,Swift语言从2014年的出现到现在,一步一步变的完善,渐渐变的受欢 ...

  7. iOS-UI-UI控件概述

    以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...

  8. ios开发中经常用到的控件

    以下是按照使用频率对ios的控件进行罗列. 1.最常用的UI控件: UIButton (按钮).UILabel (文本标签).UITextField (文本输入框).UIImageView( 图片显示 ...

  9. iOS开发UI篇—Date Picker和UITool Bar控件简单介绍

    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...

随机推荐

  1. Apple Catching(POJ 2385)

    Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9978   Accepted: 4839 De ...

  2. Service、Alarm与BroadcastReceiver的使用方法

    1:定义一个服务类,在服务类中使用AlarmManager 来管理服务的运行 public class WtacService extends Service{ private AlarmManage ...

  3. 检测页面的localstorage剩余容量

    首先用了JSON.stringify(localStorage).length得出你当前页面的localstorage所使用的字符数量current_num. 然后你再去http://arty.nam ...

  4. BZOJ 2741 【FOTILE模拟赛】L(可持久化trie)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2741 思路:我们先将a变成a的异或前缀,这样问题就变成了,在l-1到r区间内,找出i,j令a[i]^ ...

  5. WPF笔记(1.8 资源与映射)——Hello,WPF!

    原文:WPF笔记(1.8 资源与映射)--Hello,WPF! 终于,看明白了,已经是凌晨1:39分.这本书这一节写得实在是不好,一个local搞得我糊里糊涂,于是,准备按照他的思路,按照我的理解,改 ...

  6. HTML5学习摘录

    设计原理 不是规范里都包含什么,而是规范里为什么会包含它们,以及在设计这个规范的时候,设计者们是怎么看待这些东西的. 发展史:HTML2.0——>HTML3.2——>HTML4.0.1—— ...

  7. bzoj1623 [Usaco2008 Open]Cow Cars 奶牛飞车

    Description   编号为1到N的N只奶牛正各自驾着车打算在牛德比亚的高速公路上飞驰.高速公路有M(1≤M≤N)条车道.奶牛i有一个自己的车速上限Si(l≤Si≤1,000,000).     ...

  8. java使用poi创建excel文件

    import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import or ...

  9. Family Tree

    Question A traditional constructing tree problem. Given a string to represent relationships, and pri ...

  10. ZOJ 1136 Multiple (BFS)

    Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N ...