views:

698

answers:

2

Is there a way to set a default value for a picker? I save the last selected row from all the pickers and I want to be able to have the pickers load those saved rows at start up. As of now I have found this code:

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

It works but only when the user taps the picker. I need it to work when the app is first loaded. If I put this code at viewDidLoad the app will crash. Anyone know where the proper place to put this in my code to make it work?

Thank you for your time!

A: 

Here some methods from my class.

- (void)viewDidLoad {
   [super viewDidLoad];
   currentNumbersOfComponents = 1;
   //[picker selectRow:2 inComponent:0 animated:NO];
}
- (void) viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   [picker selectRow:2 inComponent:0 animated:YES];
}

#pragma mark picker view data source methods

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 5;
}

picker.dataSource is self

Morion
I did not have a method for viewDidAppear so I made one and i placed the code in there and the app crashes right at settingsPagePicke.- (void) viewDidAppear:(BOOL)animated{[settingsPagePicker selectRow:3 inComponent:0 animated:YES];[settingsPageNaturalGasPicker reloadAllComponents];}
Jeff
You don't have to reloadAllComponents to see changes after selection. And check if your component at index 0 has minimum 4 rows to allow selecting of the row with index 3.
Morion
And if it will crash anyway, post an error please.
Morion
I get:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'I need to initialize my data source first, but how can I initialize the data source with out tapping using the picker?
Jeff
This is very strange, because your picker must be completely initialized after appearing of the view. Check your picker's data source methods to be called before viewDidAppear. And one question: does your app working without setting a row in the picker?
Morion
The app works great without setting the row...
Jeff
I've just writing an example and it works fine. Can you post your class?
Morion
No can't post class (the whole confidentiality thing...My boss is real paranoid) . how can I initialize the data source with out tapping using the picker?
Jeff
Hmm. I'm confused a bit and, perhaps, just cannot understand you in right. If you have UIPicker as a subView of your view, it will call it's data source methods before your view will appear. I've just tried to place selectRow method both in viewDidLoad and viewDidAppear and all is fine. I can post my code if you want.
Morion
lol I'm confused too.. Sure it can't hurt to post your code..Thank you
Jeff
Thank you Morion for all your help. Your code looks like my code for the most part. I will look into this deeper and let you know where the problem is.
Jeff
It works now!!! It was something really small and dumb. I can't thank you enough for your help.
Jeff
You are welcome)
Morion
+1  A: 

Have you checked your data source for the settingsPagePicker before calling -selectRow:inComponent:animated to make sure there is data available at that index (3 in your sample code)?

How are you loading your data for your data source? You can initialize your data source in viewDidLoad first and then call selectRow once you know there is data available.

UPDATE: Here is what your code should look like or something like it:

- (void)viewDidLoad
{
    [super viewDidLoad];
    pickerDataSource = [[NSMutableArray alloc] init];
    [pickerDataSource addObject:@"Item 01"];
    [pickerDataSource addObject:@"Item 02"];
    [pickerDataSource addObject:@"Item 03"];
    [pickerDataSource addObject:@"Item 04"];

    // Might want to move this to -viewWillAppear:animated
    [settingsPagePicker selectRow:3 inComponent:0 animated:YES];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource objectAtIndex:row];
    }
    return @"";
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{
    if (pickerView == settingsPagePicker)
    {
        return [pickerDataSource count];
    }
    return 0;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
    return 1;
}
Matt Long
I get:Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'I need to initialize my data source first, but how can I initialize the data source with out tapping using the picker?
Jeff
It looks like your data source is an NSArray. Your error means that you are attempting to access a row in the data source that doesn't exist (yet). How are you allocating the NSArray currently? In what event? Tapping the picker shouldn't be a requirement to initialize an NSArray. Just initialize it in -viewDidLoad. Then when -pickerView:titleForRow:forComponent should return whatever object is at that index (row) in your NSArray.
Matt Long
My NSArray is being initialized in viewDidLoad. If I put a break point at the array, the app will stop there. I think i am not to something..
Jeff
Can you post your viewDidLoad code in your original question as an update? It would also help if you would post your picker delegate code.
Matt Long
It works now!!! It was something really small and dumb. I can't thank you enough for your help.
Jeff