<!--StartFragment-->
cocoa提供了UIDatePicker控件,但是对于程序员而言并不友好。其糟糕之处,莫过于无法指定其frame,它的大小固定为320*216(占据了近整个iphone屏幕的一半),导致在UI设计时很难安排下这个“巨大”的东西。
我们自定义的日期挑选控件是这样的。
它默认情况下显示为一个textfield,以文本的方式显示日期,当你试图编辑它时,会弹出一个足够巨大的UIDatePicker控件:
<!--StartFragment-->
转动日期时,其实textfield中的值会作相应改变。选择好日期后,点击左上角的关闭按钮关闭这个UIDatePicker。
这个控件的使用非常简单。
通常只需要通过下面的代码构造它并addSubview就可以了:
DatePicker* dp=[[DatePicker alloc]initWithFrame:CGRectMake (10,25,220,35)];
[self.view addSubview:dp];
如果想改变DatePicker的样式,比如显示时间而不是日期,可以修改其datePickerMode和dateFormatter属性:
dp.datePickerMode=UIDatePickerModeTime;
NSDateFormatter* df=[[NSDateFormatter alloc]init];
[df setDateFormat:@"HH:mm:ss"];
dp.dateFormatter=df;
如果要获取控件的日期时间值和字符串值,则可以使用控件的date属性和textField.text属性。
全部的源代码在这里:
============DatePicker.h===========
@interface DatePicker : UIView
<UITextFieldDelegate>{
UITextField* textField;//文本框
UIDatePicker* datePicker;//日期选择控件
NSDateFormatter *dateFormatter;//日期格式
UIDatePickerMode datePickerMode;//日期控件显示风格
NSDate* date;
UIView* subview;
}
@property(nonatomic) UIDatePickerMode datePickerMode;
//@property(nonatomic,retain)UITextField* textField;
@property(nonatomic,retain)NSDateFormatter* dateFormatter;
@property(nonatomic,retain)NSDate* date;
//@property(nonatomic,retain)UIDatePicker* datePicker;
-(UIDatePickerMode)datePickerMode;
-(void)setDatePickerMode:(UIDatePickerMode)mode;
-(NSDateFormatter*)dateFormatter;
-(void)setDateFormatter:(NSDateFormatter *)df;
-(NSDate*)date;
-(void)setDate:(NSDate*)d;
-(UITextField*)textField;
-(UIDatePicker*)datePicker;
@end
==============DatePicker.m==============
#import "DatePicker.h"
@implementation DatePicker
-(UIDatePicker*)datePicker
{
return datePicker;
}
-(UITextField*)textField
{
return textField;
}
-(NSDate*)date
{
return date;
}
-(void)setDate:(NSDate *)d
{
date=d;
datePicker.date=date;
}
-(NSDateFormatter*)dateFormatter
{
return dateFormatter;
}
-(void)setDateFormatter:(NSDateFormatter *)df{
dateFormatter=df;
textField.text=[dateFormatter stringFromDate:date];
}
-(UIDatePickerMode)datePickerMode
{
return datePickerMode;
}
-(void)setDatePickerMode:(UIDatePickerMode)mode{
datePickerMode=mode;
datePicker.datePickerMode=datePickerMode;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
//默认日期格式为yyyy-MM-dd
dateFormatter= [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//picker的默认时间为当前时间
date=[NSDate date];
//picker的默认style为只显示日期
datePickerMode=UIDatePickerModeDate;
//构造一个子视图,用于显示日期选择器
subview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
subview.backgroundColor=[UIColor clearColor];
subview.tag=0;
//为子视图构造工具栏按钮
UIBarButtonItem* item = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self action:@selector(btnCloseClick)] autorelease];
NSArray* buttons=[NSArray arrayWithObjects:item,nil];
//为子视图构造工具栏
UIToolbar *subToolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
subToolbar.barStyle = UIBarStyleBlackTranslucent;
[subToolbar sizeToFit];
[subToolbar setItems:buttons animated:YES]; //把按钮加入工具栏
[subview addSubview:subToolbar];//把工具栏加入子视图
[subToolbar release];
//为子视图构造datePicker
datePicker=[[UIDatePicker alloc]init];
[datePicker setDate:date];
datePicker.frame=CGRectMake(0, 44, 320, 216);
datePicker.datePickerMode=datePickerMode;
//指定datepicker的valueChanged事件
[datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
[subview addSubview:datePicker]; //把datePicker加入子视图
//上面是子视图,下面是父视图
//文本框
textField=[[UITextField alloc]initWithFrame:frame];
textField.delegate=self;
// textField.enabled=NO;
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.text=[dateFormatter stringFromDate:date];
[self addSubview:textField];
}
return self;
}
//当datepicker的值改变时触发
-(void)dateChanged:(id)sender{
date = [sender date];//获取datepicker的日期
//改变textField的值
textField.text=[NSString stringWithString:
[dateFormatter stringFromDate:date]];
}
//关闭按钮点击时触发
-(void)btnCloseClick{
if(subview!=nil){
subview.tag=0;
[subview removeFromSuperview];
}
}
- (void)dealloc {
[textField release];
[date release];
[dateFormatter release];
[datePicker release];
[subview release];
[super dealloc];
}
#pragma mark textField delegate method
//当textField被点击时触发
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (subview.tag==0) {//若tag标志等于0,说明datepicker未显示
//置tag标志为1,并显示子视图
subview.tag=1;
[self.superview addSubview:subview];
}
return NO;
}
@end
<!--EndFragment--> <!--EndFragment-->