日期选择代码,使用datepicker

来源:互联网 发布:海口seo公司 编辑:程序博客网 时间:2024/06/08 20:15

日期选择代码,使用datepicker

- (void)viewDidLoad

{

    [super viewDidLoad];

 //日期

    lessonDateTextField_ = [ [ UITextField alloc ] init ];

    lessonDateTextField_.frame = CGRectMake( newPoint.x , newPoint.y , dudaoTextFieldWidth , dudaoTextFieldHeight );

    lessonDateTextField_.borderStyle = UITextBorderStyleRoundedRect;

    lessonDateTextField_.font =[ UIFont fontWithName: @"Noteworthy-Light" size: 18 ];

    //文本输入框添加按钮

    UIButton* lessonDateButton = [ UIButton buttonWithType: UIButtonTypeDetailDisclosure ];

    [ lessonDateButton setImage: buttonImage forState: UIControlStateNormal ];

    [ lessonDateButton addTarget: self

                          action: @selector(lessonDateButtonDidPush) 

                forControlEvents: UIControlEventTouchUpInside ];

    lessonDateTextField_.rightViewMode = UITextFieldViewModeAlways;

    lessonDateTextField_.rightView = lessonDateButton;

    lessonDateTextField_.delegate = self;

    [ self.view addSubview: lessonDateTextField_ ];

//*------------------------------------初始化时间选择器--------------------------------------*//

    

    //日期,显示timepicker,界面整体上移

    datePicker_ = [ [ UIDatePicker alloc ] init ];

    datePicker_.frame = CGRectMake( 0 , 480 , 320 , 216 );

    datePicker_.datePickerMode = UIDatePickerModeDate;

    [ self.view addSubview: datePicker_ ];

    

    //*----------------------------------------------------------------------------------------*//

}

//日期选择按钮

- (void)lessonDateButtonDidPush

{

    //tableview都移开

    [ classTypeTableView_ removeFromSuperview ];

    [ lessonClassroomTableView_ removeFromSuperview ];

    [ lessonSectionTableView_ removeFromSuperview ];

    [ schoolDistrictTableView_ removeFromSuperview ];

    

    //设置完成按钮

    UIBarButtonItem* doneButton = 

    [ [ [ UIBarButtonItem alloc ] 

       initWithBarButtonSystemItem: UIBarButtonSystemItemDone

       target:self

       action: @selector(doneDidPush) ] autorelease ];

    [ self setToolbarItems: [ NSArray arrayWithObject: doneButton ] 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" ];

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

}

原创粉丝点击