11

I am trying to reset the UIPickerView on button click. My pickerview id is created at runtime, I have delegates set already. After googling, I found

[pickerView reloadAllComponents];

But this makes my app crash everytime it reaches here.

Object at index 0 is "Select from list" and then the items. When the submit button is clicked, I want that the "Select from list" should remain at the top of my label (selected index: 0).

Here is my code

ViewDidload

 pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;

  -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
    }


 // Total rows in our component.
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return [nameArray count];
    }

// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [nameArray objectAtIndex: row];
    }

// Do something with the selected row.

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        dlbl.hidden=YES;
        NSLog(@"You selected this: %@", [nameArray objectAtIndex: row]);
        [btnSelectDesigner setTitle:[nameArray objectAtIndex: row] forState:UIControlStateNormal]; 

    }

and, on button click:

-(IBAction)btnSubmitClicked:(id)sender{
[pickerView reloadAllComponents];
}

Any idea what I'm doing wrong?

Thanks

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
FirstTimer
  • 313
  • 1
  • 5
  • 14

3 Answers3

32
[picker reloadAllComponents];
[picker selectRow:0 inComponent:0 animated:YES];
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
9

Swift version:

picker.selectRow(0, inComponent: 0, animated: true)

rgamber
  • 5,749
  • 10
  • 55
  • 99
5
//use this line for going at the top of the label index 0:

[picker selectRow:0 inComponent:0 animated:YES];
Abhishek
  • 2,255
  • 1
  • 13
  • 21