UIDatePicker控件
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控件的更多相关文章
- iOs基础篇(二十二)—— UIPickerView、UIDatePicker控件的使用
一.UIPickerView UIPickerView是一个选择器控件,可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. 1.常用属性 (1)num ...
- iOS开发中UIDatePicker控件的使用方法简介
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Timer四 ...
- iOS学习之UIDatePicker控件使用
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. , 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Ti ...
- Swift - 日期选择控件(UIDatePicker)的用法
1,使用storyboard创建日期选择控件 首先我们将一个UIDatePicker控件和一个按钮直接添加到Main.Storyboard上.该按钮是为了点击时弹出提示框显示当前选择的日期和时间. 同 ...
- 【IOS 开发】基本 UI 控件详解 (UIDatePicker | UIPickerView | UIStepper | UIWebView | UIToolBar )
转载注明出处 : http://blog.csdn.net/shulianghan/article/details/50348982 一. 日期选择器 (UIDatePicker) UIDatePic ...
- Swift学习之熟悉控件
最近是比较清闲一些的,对于一个开发者来说,这也是一个很好的充电机会.以前做项目都是使用Objective-C去开发,但我们都知道,Swift语言从2014年的出现到现在,一步一步变的完善,渐渐变的受欢 ...
- iOS-UI-UI控件概述
以下列举一些在开发中可能用得上的UI控件: IBAction和IBOutlet,UIView 1 @interface ViewController : UIViewController 2 3 @p ...
- ios开发中经常用到的控件
以下是按照使用频率对ios的控件进行罗列. 1.最常用的UI控件: UIButton (按钮).UILabel (文本标签).UITextField (文本输入框).UIImageView( 图片显示 ...
- iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
iOS开发UI篇—Date Picker和UITool Bar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
随机推荐
- HTML+CSS学习
1.彻底弄懂CSS盒子模式(DIV布局快速入门) 2.在CSS中,BOX的Padding属性的数值赋予顺序为padding:10px; 四个内边距都是10px padding:5px 10px; 上下 ...
- python-Django环境搭建
一例中python版本使用3.5版,通常来说linux自带的python都在2.6左右,所以3.5环境要自己编译安装python 第一部分:安装python3.5 001.解决依赖问题 yum -y ...
- linux多线程编程之互斥锁
多线程并行运行,共享同一种互斥资源时,需要上互斥锁来运行,主要是用到pthread_mutex_lock函数和pthread_mutex_unlock函数对线程进行上锁和解锁 下面是一个例子: #in ...
- 验证abc三列数字符合我的小弟要求
需求好像是: 1.第一列数据有重复的找出来,并且找出它的重复位置 2.第三列根据第一列得出的位置,取出相应位置的数据进行相加 3.相加的结果 是否等同于第二列的对应位置数据 <!DOCTYPE ...
- C# 调用 Web Service
Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...
- java 常用的验证方法帮助类
import java.text.ParseException; import java.util.Collection; import java.util.Map; /** * 常用的验证方法帮助类 ...
- [C# 基础知识系列]专题九: 深入理解泛型可变性
引言: 在C# 2.0中泛型并不支持可变性的(可变性指的就是协变性和逆变性),我们知道在面向对象的继承中就具有可变性,当方法声明返回类型为Stream,我们可以在实现中返回一个FileStream的类 ...
- javac命令详解(上)
摘自http://blog.csdn.net/hudashi/article/details/7058998 javac命令详解(上) ja ...
- eclipse中如何创建maven项目
1.在eclipse中,file-->new-->maven project,勾选Create a simple project,点击next. 2.添加项目信息,点击finish.(pa ...
- Python中的深浅拷贝,赋值及引用
简单来说,若对象a中存的是列表或字典等可变对象,b对a的浅拷贝只是对对象第一层的复制,修改b第二层的元素仍然会影响两个对象. 深拷贝则是不会影响原来的对象. import copy.copy() 浅拷 ...