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显示时间的控件 有默认宽高,不用设置数据源和代理 如何 ...
随机推荐
- Wireless Network(POJ 2236)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20724 Accepted: 871 ...
- Web数据采集
http://blog.csdn.net/pqhdp/article/details/4352769 http://blog.csdn.net/CharlesSimonyi/article/detai ...
- Keil C51 与 ARM 并存的方法
很多朋友都在想,怎么让keil C51与ARM能够并存使用.有安装经验的朋友都知道,安好C51后再安ARm,C51不能正常工作:安好ARM后再安C51,ARM不能正常工作. 网上也有相关解决办法,不过 ...
- 刺猬大作战(游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4)
游戏特性[编辑] 游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4[2]. 0.9.12开始支持实时动态缩放游戏画面. 个性化[编辑] 刺猬大作战有着高度定制性 游戏模式: ...
- SQL Serverf 索引 - 索引压缩 、附加特性 <第十篇>
一.索引压缩 数据和索引压缩在SQL Server2008被引入.压缩一个索引意味着将在一个页面中获得更多的关键字信息.这可以造成重大的性能改进,因为存储索引需要的页面和索引级别更少.因为索引中的键值 ...
- C# 获取当前应用程序的绝对路径支持asp.net
Asp.net在类库中获取某文件的绝对路径.这个问题在初学的时候就经常碰到过,经常是查了忘,忘了查.浪费了大量的今天专门写个文章,以后到这里查.有时间顺便记得研究下这个东西. 在主程序目录就不说了 ...
- HDOJ-1010 Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 给出一个n*m的迷宫 X为墙 .为空地 S为起点 D为终点 给出时间T 每走一步花费一单位的时间 走过的空 ...
- unmount的时候报错
卸载存储的时候报错 device is busy 解决办法 例:/mnt/test 为存储挂载点 fuser -m -v /mnt/test fuser 可以显示出当前哪个程序在使用磁盘上的某个文件. ...
- 《Qt编程的艺术》——5.1 手动布局
在传统的GUI设计中,每个控件(Widget)都要手动地绑定在窗口之上的一个点上(也就是说,这个控件被指定成了给定GUI元素的父对象),同时还要指定这个控件的高度和宽度.作为所有图形元素的基础类,QW ...
- Lazy方式单列模式,一种线程安全模式的新选择
public class WeProxyClient { private static readonly Lazy<WeProxyClient> list = new ...