- (void)viewDidLoad
{
[super viewDidLoad];
//初始日期
beginDateTextField_ = [ [ UITextField alloc ] init ];
beginDateTextField_.frame = CGRectMake( newPoint.x , newPoint.y , dudaoTextFieldWidth , dudaoTextFieldHeight );
beginDateTextField_.borderStyle = UITextBorderStyleRoundedRect;
beginDateTextField_.font =[ UIFont fontWithName: @"Helvetica" size: 16 ];
beginDateTextField_.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//文本输入框添加按钮
beginDateButton = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];
[ beginDateButton setImage: buttonImage forState: UIControlStateNormal ];
[ beginDateButton addTarget: self
action: @selector(selectButton:)
forControlEvents: UIControlEventTouchUpInside ];
beginDateTextField_.rightViewMode = UITextFieldViewModeAlways;
beginDateTextField_.rightView = beginDateButton;
beginDateTextField_.delegate = self;
[ self.view addSubview: beginDateTextField_ ];
//初始化存放选择的textfield
selectedTextField = [ [ UITextField alloc ] init ];
}
//************优化日期选择代码***************//
//显示日期选择
- (void)dateButtonDidPush
{
//设置完成按钮
UIBarButtonItem* beginDoneButton =
[ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemDone
target: self
action: @selector(doneDidPush) ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: beginDoneButton ] animated: YES ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//时间选择器上移
datePicker_.frame = CGRectMake( 0 , 216 , 320 , 216 );
//工具条上移
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height - 50 ;
self.navigationController.toolbar.frame = toolbarFrame;
[ UIView commitAnimations ];
}
//日期选择完成按钮
- (void)doneDidPush
{
NSDate* date = [ datePicker_ date ];
NSDateFormatter* dateFormatter = [ [ NSDateFormatter alloc ] init ];
[ dateFormatter setDateFormat: @"yyyy-MM-dd" ];;
selectedTextField.text = [ dateFormatter stringFromDate: date ];
[ UIView beginAnimations: nil context: nil ];
[ UIView setAnimationDuration: 0.3 ];
//隐藏工具条
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.bounds.size.height + 65 ;
self.navigationController.toolbar.frame = toolbarFrame;
//隐藏完成按钮
UIBarButtonItem* spaceButton = [ [ [ UIBarButtonItem alloc ]
initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace
target: nil action: nil ] autorelease ];
[ self setToolbarItems: [ NSArray arrayWithObject: spaceButton ] animated: YES ];
//隐藏日期选择器
datePicker_.frame = toolbarFrame;
[ UIView commitAnimations ];
}
//判断是那一个textfield中按钮,并把相应textfield传给 selectedTextField
- (void)selectButton:(UIButton*)button
{
if ( button == beginDateButton )
{
selectedTextField = beginDateTextField_;
}
else
{
selectedTextField = endDateTextField_;
}
[ self dateButtonDidPush ];
}
//***************************************//