- (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 ];
}
//***************************************//