UIDatePicker的創(chuàng)建
UIDatePicker是一個(gè)可以用來(lái)選擇或者設(shè)置日期的控件,不過(guò)它是像轉(zhuǎn)輪一樣的控件,而且是蘋果專門為日歷做好的控件,如下圖所示:

除了UIDatePicker控件,還有一種更通用的轉(zhuǎn)輪形的控件:UIPickerView,只不過(guò)UIDatePicker控件顯示的就是日 歷,而UIPickerView控件中顯示的內(nèi)容需要我們自己用代碼設(shè)置。本篇文章簡(jiǎn)單介紹UIDatePicker控件,后邊的文章會(huì)介紹 UIPickerView。
1、運(yùn)行Xcode ,新建一個(gè)Single View Application,名稱為UIDatePicker Test,其他設(shè)置如下圖所示:

2、單擊ViewController.xib,打開(kāi)Interface Builder。拖一個(gè)UIDatePicker控件到視圖上:

3、然后拖一個(gè)按鈕在視圖上,并修改按鈕名稱為Select:

單擊按鈕后,彈出一個(gè)Alert,用于顯示用戶所作選擇。
4、創(chuàng)建映射:打開(kāi)Assistant Editor,選中UIDatePicker控件,按住Control,拖到ViewController.h中:

新建一個(gè)Outlet,名稱為datePicker:

然后以同樣的方式為按鈕建立一個(gè)Action映射,名稱為buttonPressed,事件類型為默認(rèn)的Touch Up Inside。
5、選中UIDatePicker控件,打開(kāi)Attribute Inspector,在其中設(shè)置Maximum Date比如我們這里設(shè)為2100-12-31:

實(shí)例
而今天我們要做的時(shí)間選取器成品具體效果如下:

我們自定義一個(gè)LGDatePickerView,在LGDatePickerView里面實(shí)現(xiàn)。
背景半透明:
背景是半透明的,點(diǎn)擊的灰色背景的時(shí)候,時(shí)間選取器消失。在LGDatePickerView初始化方法里,代碼如下:
復(fù)制代碼 代碼如下:
- (id)init
{
self = [super init];
if (self) {
//背景半透明,綁定取消方法
UIControl *control = [[UIControl alloc] initWithFrame:SCREEN_BOUNDS];
control.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5f];
[self addSubview:control];
[control addTarget:self action:@selector(actionCancel:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
綁定的actionCancel方法:
復(fù)制代碼 代碼如下:
- (void)actionCancel:(id)sender
{
[self removeFromSuperview];
}
確定取消按鈕:看到上面的確定取消按鈕,你會(huì)怎么做,寫一個(gè)UIView上面放兩個(gè)UIButton。這樣做也是可以實(shí)現(xiàn)的。我們還可以用UIToolbar。在LGDatePickerView初始化方法里加上下面這段代碼:
復(fù)制代碼 代碼如下:
// Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, SCREEN.height - 250, SCREEN.width, 50)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *itemCancelDone = [[UIBarButtonItem alloc] initWithTitle:@"確定" style:UIBarButtonItemStylePlain target:self action:@selector(actionConfirm:)];
UIBarButtonItem *itemCancel = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(actionCancel:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:itemCancel,space,itemCancelDone, nil]];
[control addSubview:toolbar];
actionCancel上面已經(jīng)實(shí)現(xiàn)了。下面實(shí)現(xiàn)actionConfirm方法。它有什么作用呢?點(diǎn)擊的時(shí)候獲取到時(shí)間,然后通過(guò)代理代理出去。
時(shí)間選取器消失:
復(fù)制代碼 代碼如下:
- (void)actionConfirm:(id)sender
{
if ([self.delegate respondsToSelector:@selector(datePickerView:didSelectTime:)]) {
[self.delegate datePickerView:self didSelectTime:self.datePicker.date];
}
[self removeFromSuperview];
}
代理方法:在LGDatePickerView.h
復(fù)制代碼 代碼如下:
@protocol LGDatePickerViewDelegate <NSObject>
- (void)datePickerView:(LGDatePickerView *)datepicker didSelectTime:(NSDate *)time;
@end
創(chuàng)建UIDatePicker:在LGDatePickerView.h定義一個(gè)全局變量
復(fù)制代碼 代碼如下:
@property (nonatomic, strong) UIDatePicker *datePicker;
在LGDatePickerView初始化方法里加上下面這段代碼:
復(fù)制代碼 代碼如下:
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.backgroundColor = [UIColor whiteColor];
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
datePicker.date = [NSDate date];
datePicker.frame = CGRectMake(0, SCREEN.height - 200, SCREEN.width, 220);
[control addSubview:datePicker];
self.datePicker = datePicker;
使用LGDatePickerView使用起來(lái)很簡(jiǎn)單,創(chuàng)建一下,然后加載self.view上面即可:
復(fù)制代碼 代碼如下:
LGDatePickerView *datePicker = [[LGDatePickerView alloc] init];
datePicker.delegate = self;
datePicker.datePicker.date = [NSDate date];
datePicker.frame = self.view.bounds;
[self.view addSubview:datePicker];