UIDatePicker to show year only

前端 未结 6 513
时光取名叫无心
时光取名叫无心 2020-12-07 00:51

Is there a way to have a UIDatePicker show year only?

6条回答
  •  既然无缘
    2020-12-07 01:58

    I cannot comment, but I was playing around with the answer by Wolvorin and I decided to do a way where the most recent year would be at the top of the Picker. Currently, his code goes oldest to most recent years.

    All I changes was in my NSMutableArray setup in the viewDidLoad instead of his:

    //Create Years Array from 1960 to This year
    years = [[NSMutableArray alloc] init];
    for (int i=1960; i<=i2; i++) {
        [years addObject:[NSString stringWithFormat:@"%d",i]];
    }
    

    I used:

    years = [[NSMutableArray alloc] init];
    for (int i1=i2; i1<=i2 & i1>=1920; i1--) {
        [years addObject:[NSString stringWithFormat:@"%d",i1]];
    }
    

    Which does the for in reversed order going from the most recent date, then subtracting one year (and adding it to the Array) until it gets to 1920.

    My formula:

    int i1=i2; i1<=i2 & i1>=1920; i1--
    

提交回复
热议问题