显示8年前的datepicker并在ios中锁定ui datepicker

时间:2012-10-12 06:54:26

标签: iphone ios xcode

在我的项目中,我需要显示日期选择器18,我需要锁定80年后,所以如何在datepicker中显示这些日期可以帮助我在这里找到任何人 这里正在添加我的代码我在日志中打印的代码,但我需要在我的datepicker上显示,以便我如何显示

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

self.datePickerView.minimumDate = minDate;
self.datePickerView.maximumDate = maxDate;

NSLog(@"minDate   %@", minDate);
NSLog(@"maxDate%@", maxDate);

2 个答案:

答案 0 :(得分:21)

首先,最短的日期必须是100年的回溯日期,最长的日期必须是18年的回溯日期,然后将pickerView的日期设置为日期范围之间的日期,以便最终的代码看起来像

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

self.datePickerView.minimumDate = minDate;
self.datePickerView.maximumDate = maxDate;
self.datePickerView.date = minDate;

希望这会对你有所帮助。

答案 1 :(得分:2)

这是view controller类的viewDidLoad方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


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



    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    [pickerView setMinimumDate:minDate];
    [pickerView setMaximumDate:maxDate];
    [pickerView setDate:minDate];
    [[self view] addSubview:pickerView];

}

我的输出

enter image description here

你可以把你的输出分享为截图吗?