UIDatePicker控件


UIDatePicker繼承關系如下:

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

 


 

1、創建UIDatePicker

創建一個UIDatePicker控件並顯示出來來看看這玩意長什么模樣,代碼:

1     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
2     [self.view addSubview:datePicker];//添加到當前的視圖中顯示出來

樣子如下:

IOS6下UIDatePicker的樣子IOS7下UIDatePicker的樣子

2、配置UIDatePicker

2.1 UIDatePicker國際化

我們看見默認的UIDatePicker是英文的,這讓我很不爽,用下面這個方法讓它變成中文

1 @property(nonatomic, retain) NSLocale *locale

 

 下面趕緊用這個方法給UIDatePicker對象設置上讓它變成中文,英文看着實在不爽:

1     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
2     NSLocale *chineseLocale = [NSLocale localeWithLocaleIdentifier:@"zh_cn"]; //創建一個中文的地區對象
3     [datePicker setLocale:chineseLocale]; //將這個地區對象給UIDatePicker設置上
4     [self.view addSubview:datePicker];//添加到當前的視圖中顯示出來

 

設置好了,來看看效果吧

IOS6下中文的UIDatePicker樣子IOS7下中文的UIDatePicker樣子

恩,現在UIDatePicker變成中文了,但是有個問題,不管這個IOS系統是什么語言,它總是顯示成中文,不利於國際化,下面我們修改代碼實現UIDatePicker的國際化

1     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
2     NSLocale *currentLocale = [NSLocale currentLocale]; //取得當前IOS用戶所設置的地區信息
3     [datePicker setLocale:currentLocale]; //給UIDatePicker對象設置地區信息
4     [self.view addSubview:datePicker];//添加到當前的視圖中顯示出來

 

使用以上代碼后有可能程序顯示的效果還是英文的,這需要設置你的IPhone的International信息才可以。

IOS6下的International信息設置 IOS7下的International信息設置

如上圖我設置的是語言為英文,下面的地區格式設置為中國,日歷為公歷(吐槽蘋果都IOS7了,中國農歷還是沒實現,連小日本的日歷都有,這是什么意思),我的程序顯示效果如下:

IOS6下設置完International的顯示效果IOS7下設置完International的顯示效果

 2.2 UIDatePicker顯示模式

 用於配置UIDatePicker顯示模式的方法如下

1 @property(nonatomic) UIDatePickerMode datePickerMode

 

 該方法用於配置UIDatePicker是只顯示日期選擇、時間選擇、或者既有日期又有時間選擇(默認就是這種),還是顯示為一個倒計時器。

UIDatePickerMode是個枚舉分別對應那四種方法的值。定義如下:

1 typedef enum {
2     UIDatePickerModeTime, //只有時間選擇的模式
3     UIDatePickerModeDate, //只有日期選擇的模式
4     UIDatePickerModeDateAndTime, //既有時間又有日期的選擇模式(默認這種)
5     UIDatePickerModeCountDownTimer //倒計時器模式
6 } UIDatePickerMode;

 

由於上面三種模式都是默認模式的變體,比較好理解,下面只實現一下倒計時器,看看啥子模樣,

1     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
2     [datePicker setDatePickerMode:UIDatePickerModeCountDownTimer]; //設置顯示模式是一個倒計時器
3     [self.view addSubview:datePicker];//添加到當前的視圖中顯示出來

 

顯示效果為:

IOS6下倒計時器的顯示效果IOS7下倒計時器的顯示效果

3、UIDatePicker的事件處理

3.1 普通形態的UIDatePicker添加事件

上面的方法用於創建和配置UIDatePicker的顯示模式,UIDatePicker也是繼承與UIControl的因此也可以使用

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

 

下面來給UIDatePicker添加一個事件,當用戶改變了UIDatePicker的值時就彈出一個警告框顯示用戶選擇的日期及時間,下面是初始化以及添加事件的代碼:

1     UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
2     [datePicker addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventValueChanged]; //為UIDatePicker添加一個事件當UIDatePicker的值被改變時觸發
3     [[self view] addSubview:datePicker];//添加到當前的視圖中顯示出來

 

下面是事件處理方法:

1 - (void)changeTime:(UIDatePicker*)sender
2 {
3     NSDateFormatter * df = [[NSDateFormatter alloc] init]; //初始化一個日期格式化器
4     [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]]; //初始化一個中國的區域信息對象
5     [df setDateStyle:NSDateFormatterFullStyle]; //設置日期格式化器的日期顯示樣式為完整樣式
6     [df setTimeStyle:NSDateFormatterFullStyle]; //設置日期格式化器的事件顯示樣式為完整樣式
7     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIDatePicker事件" message:[df stringFromDate:[sender date]] delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];//初始化一個UIAlertView用戶顯示用戶選擇的日期及時間
8     [alert show];//顯示這個UIAlertView
9 }

 

下面來看效果:

IOS6中UIDatePicker的事件綁定效果IOS7中UIDatePicker的事件綁定效果

 3.2 倒計時器形態的UIDatePicker添加事件

這里用倒計時器的模式以及計時器對象做一個倒計時程序,下面貼出完整的ViewController代碼,首先是頭文件:

1 #import <UIKit/UIKit.h>
2 
3 @interface ZFCViewController : UIViewController
4 
5 @property (nonatomic,strong) NSTimer *timer; //計時器對象
6 @property (nonatomic,strong) UIDatePicker *datePicker; //倒計時選擇器對象
7 @property (nonatomic,assign) double leftSeconds; //倒計時選擇器對象剩余多少秒
8 
9 @end

 

接着是實現文件:

 1 //
 2 //  ZFCViewController.m
 3 //  UIDatePickerTest
 4 //
 5 //  Created by 趙 福成 on 14-5-19.
 6 //  Copyright (c) 2014年 ZhaoFucheng. All rights reserved.
 7 //
 8 
 9 #import "ZFCViewController.h"
10 
11 @interface ZFCViewController ()
12 
13 @end
14 
15 @implementation ZFCViewController
16 
17 NSString *msg; //用於顯示還有多少秒的信息變量
18 NSDateFormatter *df; //用於格式化日期的顯示格式
19 UIAlertView *alert; //用於循環時彈出的警告框
20 
21 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
22 {
23     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
24     if (self) {
25         // Custom initialization
26     }
27     return self;
28 }
29 
30 - (void)loadView
31 {
32     UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
33     [self setView:rootView];
34     self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//創建一個UIDatePicker對象
35     [self.datePicker setDatePickerMode:UIDatePickerModeCountDownTimer]; //設置為倒計時器
36     [self.datePicker addTarget:self action:@selector(changeTime:) forControlEvents:UIControlEventValueChanged]; //為UIDatePicker添加一個事件當UIDatePicker的值被改變時觸發
37     [[self view] addSubview:self.datePicker];//添加到當前的視圖中顯示出來
38 }
39 
40 - (void)changeTime:(UIDatePicker*)sender
41 {
42     [self setLeftSeconds:[sender countDownDuration]];//取得這個UIDatePicker倒計時器還剩多少秒
43     [sender setEnabled:NO];//當觸發了一個事件就將UIDatePicker設置為禁用
44     [self setTimer:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTimer) userInfo:nil repeats:YES]];//設置了一個計時器對象,每隔一秒執行一次
45 }
46 
47 - (void)countDownTimer
48 {
49     self.leftSeconds -= 1; //每次執行減去一秒
50     if (self.leftSeconds <= 0) { //如果時間為0了
51         [self.timer invalidate]; //將取消計時器
52         [self.datePicker setEnabled:YES]; //將UIDatePicker設置為可用
53     }
54     msg = [NSString stringWithFormat:@"還剩%g秒",self.leftSeconds]; //修改UIAlertView上的剩余時間提示信息
55     [alert setMessage:msg]; //將信息放到UILaterView上
56     if (!alert.visible) { //這個UIAlertView是否已經顯示
57         [alert show];//如果沒有顯示就顯示這個UIAlertView
58     }
59 }
60 
61 - (void)viewDidLoad
62 {
63     [super viewDidLoad];
64     // Do any additional setup after loading the view.
65     df = [[NSDateFormatter alloc] init]; //初始化一個日期格式化器
66     [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]]; //初始化一個中國的區域信息對象
67     [df setDateStyle:NSDateFormatterFullStyle]; //設置日期格式化器的日期顯示樣式為完整樣式
68     [df setTimeStyle:NSDateFormatterFullStyle]; //設置日期格式化器的事件顯示樣式為完整樣式
69     alert = [[UIAlertView alloc] initWithTitle:@"UIDatePicker事件" message:msg delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];//初始化一個UIAlertView用戶顯示用戶選擇的日期及時間
70 }
71 
72 - (void)didReceiveMemoryWarning
73 {
74     [super didReceiveMemoryWarning];
75     // Dispose of any resources that can be recreated.
76 }
77 
78 /*
79 #pragma mark - Navigation
80 
81 // In a storyboard-based application, you will often want to do a little preparation before navigation
82 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
83 {
84     // Get the new view controller using [segue destinationViewController].
85     // Pass the selected object to the new view controller.
86 }
87 */
88 
89 @end

 

這樣就做好了一個倒計時程序,效果圖如下:

IOS6下倒計時器事件演示IOS7下倒計時器事件演示

 

先到這里、以后繼續添加。。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM