Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At final i wrote my logic to achieve it.</p> <p>But there may be a better way of logic to optimise memory.</p> <p>Below is my code.</p> <p><strong>In viewdidload:</strong> </p> <pre><code> currentDateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; //Array for picker view monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil]; 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:[currentDateComponents month]-1 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; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"[currentDateComponents month]--&gt;%d&lt;--",[currentDateComponents month]); NSLog(@"--&gt;%d&lt;--",row); NSLog(@"row-&gt;%@&lt;--",[yearsArray objectAtIndex:row]); NSLog(@"--&gt;%@&lt;--",[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]]); if ([pickerView selectedRowInComponent:0]+1&lt;[currentDateComponents month] &amp;&amp; [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year]) { [pickerView selectRow:[currentDateComponents month]-1 inComponent:0 animated:YES]; NSLog(@"Need to shift"); } if (component==1) { [pickerView reloadComponent:0]; } } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor blackColor]; label.font = [UIFont fontWithName:@"Arial-BoldMT" size:17]; label.text = component==0?[monthsArray objectAtIndex:row]:[yearsArray objectAtIndex:row]; if (component==0) { if (row+1&lt;[currentDateComponents month] &amp;&amp; [[yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]==[currentDateComponents year]) { label.textColor = [UIColor grayColor]; } } return label; } // 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>Here is my output</strong></p> <p>for 2013 year:</p> <p><img src="https://i.stack.imgur.com/NxZPm.png" alt="enter image description here"></p> <p>for Other years:</p> <p><img src="https://i.stack.imgur.com/gk2Zt.png" alt="enter image description here"></p> <p>.</p> <p>.</p> <p>. <strong>Thanks for your contribution.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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