選擇日期範圍,同時要判斷選擇的開始日期和結束日期的選擇是否正確
//將字串轉為日期,為下一步比較
- (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 ];
}
}