1

I need the last selected row in my pickerview to be totally recalled when the view appears! I have this code in my viewDidAppear it animates to the last selected row but it doesn't really call the delegate and the NSLog in my rows won't print without touching my picker and reselect them.

How do I do that?

- (void)viewDidAppear:(BOOL)animated {

 NSUserDefaults *pickerViewSelectionDefaults = [NSUserDefaults standardUserDefaults];
[tasbeehPicker selectRow:[pickerViewSelectionDefaults integerForKey:@"picker"]
      inComponent:0 animated:YES];

[pickerViewSelectionDefaults synchronize];
[UIPicker reloadAllComponents];    

NSLog(@"Last selcted row was %d  ",[[NSUserDefaults standardUserDefaults] integerForKey:@"picker"]);

}
user1949873
  • 460
  • 7
  • 17

2 Answers2

1

The delegate is not supposed to be called when you update the picker through code. The delegates are only called when the user interacts with the picker. If you need the same code called in both cases then do something like this:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [myPicker selectRow:4 inComponent:0 animated:animated];
    [self handlePickerSelection:myPicker];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self handledPickerSelection:pickerView];
}

- (void)handlePickerSelection:(UIPickerView *)pickerView {
    // process selection
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks, but I see you have chosen `row` "4"! I have used the `NSUserDefault` specifically to recall the user's last selected row! can I do that? – user1949873 Apr 30 '13 at 05:31
  • I was just being lazy when typing my answer. You use of `NSUserDefaults` is fine. Though there is no reason to call `synchronize` when you read. – rmaddy Apr 30 '13 at 05:56
  • I did actually try it out with the `NSUserDefault` but the output returned calling the first row while it was the 3rd row selected!! what was wrong? – user1949873 Apr 30 '13 at 06:04
  • That's a completely separate question. To help debug that issue, store the result from `NSUserDefaults` in a separate variable so you can see what you are actually getting. – rmaddy Apr 30 '13 at 06:07
  • How do I do that? do you have a link to a similar issue? or you prefer I ask it as a new question? – user1949873 Apr 30 '13 at 06:14
0

You can get selected row from below method,

Add this code in viewDidAppear,

  int index = [pickerViewObject selectedRowInComponent:0];
  [pickerViewObject selectRow:index inComponent:0 animated:YES];

selectedRowInComponent will give you selected row number, and then you can use selectRow:Row_number inComponent:Component_number animated:YES to select any row, picker delegate method will not get called.

If you want to save selected row in NSUserDefaults than you can do in in viewWillDisappear,

  int index = [pickerViewObject selectedRowInComponent:0];

now save this index value in NSUserDefaults.

Vishwa Patel
  • 484
  • 2
  • 10