日期範圍選擇公用函數,傳參為選擇的textField

來源:互聯網
上載者:User

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

}

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.