How to select/set a selected row in picker view as default selected row every time

iOS dev picture iOS dev · Mar 7, 2012 · Viewed 14k times · Source

I have a picker view

[picker selectRow:3 inComponent:0 animated:YES];

when i wrote this code in view did load,

This method is used to set the row 3 as default row in picker.

But i want that the row i have selected is set to be as default for the next time.

what to do? thanks in advance...

Answer

Raj Subbiah picture Raj Subbiah · Mar 7, 2012

Try like this

NSUserDefaults *usrDefaults = [NSUserDefaults standardUserDefaults]; 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    [usrDefaults setInteger:row forKey:@"Index"];
}

[pickerView selectRow:[[NSUserDefaults standardUserDefaults]integerForKey:@"Index"] inComponent:0 animated:NO];