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显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
随机推荐
- Network Saboteur(搜索)
Network Saboteur POJ2531 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10351 Accept ...
- 优盘文件系统(FOR C)
优盘上的数据按照其不同的特点和作用大致可分为5 部分:MBR 区.DBR 区.FAT 区.FDT区和DATA 区. 主引导记录(MBR) 绝对扇区号为:MBR_LBA=0x00000000 处是主引导 ...
- SPI 四种模式
SPI时钟极性CPOL, = 0表示在没有数据传输时为低电平,= 1表示没有数据传输时为高电平. SPI时钟相位CPHA,= 0表示时钟的第一个沿更新数据.第二个沿锁存数据,= 1表示时钟的第一个沿锁 ...
- 主要协议SCSI、FC、iSCSI
一.SCSI SCSI是小型计算机系统接口(Small Computer System Interface)的简称,于1979首次提出,是为小型机研制的一种接口技术,现在已完全普及到了小型机,高低端服 ...
- 【转】 linux内核移植和驱动添加(三)
原文网址:http://blog.chinaunix.net/uid-29589379-id-4708909.html 原文地址:linux内核移植和驱动添加(三) 作者:genehang 四,LED ...
- codecomb 2098【stone】
题目描述 Description n个石堆围成一圈,提供两种操作: 1.每次将[L,R]堆的石子数量+k,其中,1<=L,R<=n,k>=0. 2.询问有最多石子的那一堆有多少石子. ...
- uva 101 by sixleaves
这是一道很好的模拟题,用vector<int> p[maxn],建立模型,映射为maxn个堆.主要要掌握vector模拟堆操作的简单方法.接下来得思路是自顶向下的方式,逐步完善程序.首先根 ...
- hdu 1502 Regular Words_高精度+dp
题意:问按规则排成的串有多少个A(c)>= B(c) >= C(c) 思路:因为写大整数太累,就偷懒了一下直接用java水过 import java.math.BigInteger; im ...
- Java专项面试训练(一)
1.在Java中,( )类提供定位本地文件系统,对文件或目录及其属性进行基本操作( D ) A.FileInputStream B.FileReader C.FileWriter D.File解析:F ...
- go 学习笔记 - sublime text 环境配置
园里已经有了一篇相当不错的配置说明文章,只是现在gosublime不再支持2.x.文章里的操作在sublimetext3 里一样可以使用 文章地址 : http://www.cnblogs.com/s ...