3

在我的项目中,我需要显示日期 18 并锁定我尝试过的 100 年,但我知道了,但这里锁定了那一年(1912 年)的日期和月份,我还需要显示那一年的所有日期和月份( 1912)还有一件事,当我选择日期时它与时间一起显示我不想显示时间任何人都可以解决这个问题检查当年的屏幕截图 显示日期和时间

 -(IBAction)donePickerView:(id)sender
 {

 NSDate *minDate1 =[datePickerView date];
NSString *theDate = [NSString stringWithFormat:@"%@",minDate1];

 self.dob.text = theDate;
 DatePicView.hidden = TRUE;
  datePickerView.hidden =TRUE;


}
-(void)textFieldDidBeginEditing:(UITextField *)textField {  
   [self scrollViewToCenterOfScreen:textField];
   scrlView.scrollEnabled = FALSE;
    if (textField == dob) 
{
   DatePicView.hidden = FALSE;
   datePickerView.hidden =FALSE;
   NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];

   NSDate * currentDate = [NSDate date];


   NSDateComponents * comps = [[NSDateComponents alloc] init];
   [comps setYear: -18];
   NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];
   [comps setYear: -100];
   NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0];

      [datePickerView setMinimumDate:minDate];
   [datePickerView setMaximumDate:maxDate];
   [datePickerView setDate:minDate];
         [dob resignFirstResponder];

       }
}
4

1 回答 1

1

您将 maxDate 设置为 -100 年。

所以 maxDate 比 minDate 低,这些应该反过来吗?

NSLog(@"Now: %@", currentDate);
NSLog(@"Max: %@", maxDate);
NSLog(@"Min: %@", minDate);


Now: 2012-10-18 10:04:43 +0000
Max: 1994-10-18 11:04:43 +0000
Min: 1912-10-18 11:45:11 +0000

如果您选择 1980-12-12,这将低于最大日期并高于最小日期。

UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
//  add this to display date only.
[pickerView setDatePickerMode:UIDatePickerModeDate];

[pickerView setMinimumDate:minDate];
[pickerView setMaximumDate:maxDate];
[pickerView setDate:minDate];
[[self view] addSubview:pickerView];
[pickerView setDatePickerMode:UIDatePickerModeDate];
于 2012-10-18T10:06:59.243 回答