【问题标题】:How to fix the maximum year in UIDatePicker but not date and month?如何修复 UIDatePicker 中的最大年份而不是日期和月份?
【发布时间】:2015-08-12 12:35:52
【问题描述】:

我可以通过 UIPicker 中的 [NSDate date] 设置最大日期。但是有没有办法只设置最大年份。还有一件事我希望日期和月份滚动,但年份最多只能到 2015 年

【问题讨论】:

标签: ios objective-c uipickerview uidatepicker


【解决方案1】:

有UIDatePicker的属性

@property(nonatomic, retain) NSDate *maximumDate

更多房产请访问link

【讨论】:

    【解决方案2】:
      //Get Current Date
        NSDate *currentTime = [NSDate date];
        [DatePicker setMinimumDate:currentTime];
    

    尝试设置最大日期,它将完全设置最大值为年份

        NSString *dateString = @"01-02-2010";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        // this is imporant - we set our input date format to match our input string
        // if format doesn't match you'll get nil from your string, so be careful
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
        NSDate *dateFromString = [[NSDate alloc] init];
    
        dateFromString = [dateFormatter dateFromString:dateFromString];
        [datePicker setMaximumDate: dateFromString];    
    

    【讨论】:

      【解决方案3】:

      在属性检查器中,您可以选择设置最大值。

      【讨论】:

        【解决方案4】:

        我认为这会对你有所帮助

        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *currentDate = [NSDate date];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        [comps setYear:30];
        NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
        [comps setYear:-30];
        NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
        
        [datePicker setMaximumDate:maxDate];
        [datePicker setMinimumDate:minDate];
        

        【讨论】:

          猜你喜欢
          • 2017-03-25
          • 2014-07-17
          • 2011-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-18
          • 2021-12-16
          • 1970-01-01
          相关资源
          最近更新 更多