Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prepare UIPickerView with months and years in ios?
    primarykey
    data
    text
    <p>I'm working on Preparing UIPickerView with months and year in iOS.</p> <p>Below is my code.</p> <p>in <strong>viewdidload</strong> :</p> <pre><code>//Array for picker view monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; NSString *yearString = [formatter stringFromDate:[NSDate date]]; yearsArray=[[NSMutableArray alloc]init]; for (int i=0; i&lt;13; i++) { [yearsArray addObject:[NSString stringWithFormat:@"%d",[yearString intValue]+i]]; } myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)]; myPickerView.delegate = self; myPickerView.showsSelectionIndicator = YES; [myPickerView selectRow:0 inComponent:0 animated:YES]; [self.view addSubview:myPickerView]; </code></pre> <p><strong>Picker view delegate methods:</strong></p> <pre><code>// tell the picker how many components it will have - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } // tell the picker how many rows are available for a given component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSInteger rowsInComponent; if (component==0) { rowsInComponent=[monthsArray count]; } else { rowsInComponent=[yearsArray count]; } return rowsInComponent; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString * nameInRow; if (component==0) { nameInRow=[monthsArray objectAtIndex:row]; } else if (component==1) { nameInRow=[yearsArray objectAtIndex:row]; } return nameInRow; } // tell the picker the width of each row for a given component - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { CGFloat componentWidth ; if (component==0) { componentWidth = 100; } else { componentWidth = 100; } return componentWidth; } </code></pre> <p><strong>And i got the following output :</strong></p> <p><img src="https://i.stack.imgur.com/q4D9V.png" alt="PickerImage"></p> <p><strong>But in the current year , the months from Jan to Oct have expired. How to disable those years in my picker only for the current year dynamically. Those months should be available for the remaining years.</strong></p> <p>Actually the real output is,</p> <p><img src="https://i.stack.imgur.com/ZNph2.png" alt="enter image description here"> </p> <p>In above, the expired months in the current year should be disabled in UI.</p> <p><strong><em>Any comments or suggestions would be appreciated.</em></strong></p> <p><strong><em>Thank you in advance.</em></strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload