UIPickerView and NSUserDefaults issue

Discussion in 'iPhone/iPad Programming' started by guydor, Aug 8, 2010.

  1. macrumors member

    Joined:
    Mar 10, 2009
    #1
    Hi,

    I have an UIPickerView with 2 components. I would like to save the user's selection and to apply it next time the pickerView is shown again.

    Here's my code:

    Code:
        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
        NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
        [pickerViewSelectionDefaults setInteger:row forKey:@"pickerViewSelectionKey"];
        [pickerViewSelectionDefaults synchronize];
    
    }
    
    }
    
    And...

    Code:
    - (void)viewWillAppear:(BOOL)animated { 
    
        NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
        [pickerView selectRow:[pickerViewSelectionDefaults integerForKey:@"pickerViewSelectionKey"] inComponent:0 animated:YES];
    
    }
    
    The problem is that this NSUserDefaults settings don't save the UIPickerView selections.
    My pickerView has 2 components.

    Thanks
     

Share This Page