Question

I have a perfectly working uipickerview in a uipopover with a done and cancel button. I am able to save the last selected row and display it as default when the user opens the uipickerview more than once. The problem is that, when the user logs out and logs in, I want the uipickerview to start afresh ie., when I open uipickerview and click done button (without selecting any row), I want it to go with the default row ie. row0 but it is showing up the last selected row before logout!! How can I solve this?? Help much appreciated.

NSString *selectedType = @"ALL";
int rowselected = 0;

- (void)viewDidLoad
{
    [super viewDidLoad];
    rowselected = 0;
    _typearray = [[NSArray alloc] initWithObjects:@"ALL", @"COACH", @"GROUP", @"PRIVATE", nil];
    NSInteger lastrow =[[[NSUserDefaults standardUserDefaults] objectForKey:@"gueststyperow"] integerValue];

    if(lastrow!=0)
        [self.picktype selectRow:lastrow inComponent:0 animated:NO];
    else
        [self.picktype selectRow:0 inComponent:0 animated:NO];
}

//DONE BUTTON PRESSED:

- (IBAction)doneButtonPress:(UIBarButtonItem *)sender {
    NSLog(@"%@",_delegate);
    NSNumber *temprow = [NSNumber numberWithInt:rowselected];
    [[NSUserDefaults standardUserDefaults] setValue:temprow forKey:@"gueststyperow"];
    [_delegate sendtype:selectedType];
}

//PICKERVIEW SELECTION

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
        inComponent:(NSInteger)component{
        rowselected = row;
        selectedType = [_typearray objectAtIndex:row];
}
Was it helpful?

Solution

Put this above [super viewDidLoad]:

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

That should set your pickerview to row the 0 index every time the view is loaded.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top