日期范围选择公用函数,传参为选择的textField

来源:互联网 发布:人工智能是什么东西 编辑:程序博客网 时间:2024/06/05 09:06

- (void)viewDidLoad

{

    [superviewDidLoad]; 

    //初始日期

    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 ];

    [ beginDateButtonsetImage: buttonImage forState: UIControlStateNormal ];

    [ beginDateButton addTarget: self

                         action: @selector(selectButton:) 

                forControlEvents:UIControlEventTouchUpInside ];

    beginDateTextField_.rightViewMode =UITextFieldViewModeAlways;

    beginDateTextField_.rightView =beginDateButton;

    beginDateTextField_.delegate =self;

    [ self.viewaddSubview: beginDateTextField_ ];


    //初始化存放选择的textfield

    selectedTextField = [ [ UITextField alloc ] init ];


}


//************优化日期选择代码***************//


//显示日期选择

- (void)dateButtonDidPush

{

    //设置完成按钮

    UIBarButtonItem* beginDoneButton = 

    [ [ [ UIBarButtonItem alloc

       initWithBarButtonSystemItem:UIBarButtonSystemItemDone

       target: self

       action: @selector(doneDidPush) ]autorelease ];

    [ selfsetToolbarItems: [ NSArrayarrayWithObject: beginDoneButton ] animated:YES ];

    [ UIViewbeginAnimations: nilcontext: nil ];

    [ UIViewsetAnimationDuration: 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;

    

    [ UIViewcommitAnimations ];

}


//日期选择完成按钮

- (void)doneDidPush

{

    NSDate* date = [ datePicker_date ];

    NSDateFormatter* dateFormatter = [ [ NSDateFormatter alloc ] init ];

    [ dateFormatter setDateFormat: @"yyyy-MM-dd" ];;

    selectedTextField.text = [ dateFormatterstringFromDate: date ];

    [ UIViewbeginAnimations: nilcontext: nil ];

    [ UIViewsetAnimationDuration: 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 ];

    [ selfsetToolbarItems: [ NSArrayarrayWithObject: spaceButton ] animated:YES ];

    //隐藏日期选择器

    datePicker_.frame = toolbarFrame;

    [ UIViewcommitAnimations ];

}


//判断是那一个textfield中按钮,并把相应textfield传给 selectedTextField

- (void)selectButton:(UIButton*)button

{

    if ( button == beginDateButton )

    {

        selectedTextField =beginDateTextField_;

    }

    else

    {

        selectedTextField =endDateTextField_;

    }

    [ selfdateButtonDidPush ];

}


//***************************************//





原创粉丝点击