Search code examples
iosxcodeuipickerview

add value into NSArray with looping xcode


My client does not want date of birth input with three separate UIPickerView thus I may need to create three picker view and input value into it. For Birth Date and Month is no problem as adding like that

pickerArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10",
                        @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20",
                        @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31"];

But for Birth Year, I don't need to spend my time to manually add year value into UIPickerView. I want to set "start year" and "end year" into loop and add these into UIPickerView. It can be feasible to do?


Solution

  • Just take a mutableArray

    -(NSMutableArray *)arrayWithYear:(int)startDate andEnddate:(int)endDate {
        NSMutableArray *yearArray = [NSMutableArray array];
        for (int i=startDate; i<=endDate; i++) {
           [yearArray addObject:[NSString stringWithFormat:@"%i",i]];
       }
        return yearArray;
    }
    

    And just call the function

    NSArray *myYearArray = [self arrayWithYear:1975 andEnddate:2005]