您的位置:首页 > 其它

选择日期范围,同时要判断选择的开始日期和结束日期的选择是否正确

2013-03-07 19:41 615 查看
选择日期范围,同时要判断选择的开始日期和结束日期的选择是否正确

//将字符串转为日期,为下一步比较

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

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐