選擇日期範圍,同時要判斷選擇的開始日期和結束日期的選擇是否正確

來源:互聯網
上載者:User

選擇日期範圍,同時要判斷選擇的開始日期和結束日期的選擇是否正確

//將字串轉為日期,為下一步比較

- (NSDate*)stringToDate: (NSString*)string

{

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

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

    NSDate* date = [ dateFormatter dateFromString: string ];

//        NSLog( @"date %@" , date );

    [ dateFormatter release ];

    return date;

}

//++++++++++++++++++++++++尋找按鈕等方法++++++++++++++++++++++++++++++++++++//

//點擊尋找按鈕後

- (void)searchMessage 

{

    //================ 先隱藏未隱藏的時間選取器 =====================//

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

    //==========================================================//

    

    

    if ( [ beginDateTextField_.text length ] && [ endDateTextField_.text length ] ) 

    {

        

        //********** 時間比較 *************************//

        

        NSDate* beginDate = [ self stringToDate: beginDateTextField_.text ];

        NSDate* endDate = [ self stringToDate: endDateTextField_.text ];

        //如果開始日期比結束日期前的話,才執行下一步,否則顯示警告框

        if ( NSOrderedAscending == [ beginDate compare: endDate ] )

        {

            //執行下一步

............

         }

         else  //日期範圍錯誤

         {

             UIAlertView* errorAlert = [ [ UIAlertView alloc ] initWithTitle: @"選擇日期範圍錯誤" message: @"請選擇正確日期範圍" delegate: self cancelButtonTitle: @"確定" otherButtonTitles: nil , nil ];

             [ errorAlert show ];

             [ errorAlert release ];

         }

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

    }

    else      //未選擇日期範圍,存在未選日期

    {

        UIAlertView* errorAlert = [ [ UIAlertView alloc ] initWithTitle: @"未選擇日期範圍" message: @"請選擇日期範圍" delegate: self cancelButtonTitle: @"確定" otherButtonTitles: nil , nil ];

        [ errorAlert show ];

        [ errorAlert release ];

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.