UIDatePicker繼承關係以下:app
UIDatePicker-->UIControl-->UIView-->UIResponder-->NSObjectatom
一、建立UIDatePickerspa
建立一個UIDatePicker控件並顯示出來來看看這玩意長什麼模樣,代碼:3d
1 UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];//建立一個UIDatePicker對象 2 [self.view addSubview:datePicker];//添加到當前的視圖中顯示出來
樣子以下:code
二、配置UIDatePickerorm
2.1 UIDatePicker國際化對象
咱們看見默認的UIDatePicker是英文的,這讓我很不爽,用下面這個方法讓它變成中文blog
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];//添加到當前的視圖中顯示出來
設置好了,來看看效果吧事件
恩,如今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信息才能夠。
如上圖我設置的是語言爲英文,下面的地區格式設置爲中國,日曆爲公曆(吐槽蘋果都IOS7了,中國農曆仍是沒實現,連小日本的日曆都有,這是什麼意思),個人程序顯示效果以下:
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];//添加到當前的視圖中顯示出來
顯示效果爲:
三、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 }
下面來看效果:
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
這樣就作好了一個倒計時程序,效果圖以下:
先到這裏、之後繼續添加。。