Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Set selected row of UIPickerView in applicationDidFinishLaunching

kieran12kieran12 Posts: 472Registered Users @ @
edited May 2011 in iOS SDK Development
Hi all,

I have a UIPickerView populated with an array. I save the users selection and load it at the app launch using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[NSString stringWithFormat:@"%@", fav] forKey:@"fav"];
	[defaults synchronize];

and
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	fav = [defaults objectForKey:@"fav"];

When the application launches I want the row the user has saved to be automatically selected in the UIPickerView.

I think that I might be saving the wrong information to do this but I'm not really sure.

Any help appreciated.

Thanks.
Post edited by kieran12 on
<a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
· ·

Replies

  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    ok I've made some progress. I store the value of the selected row using:
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    	[defaultsPicker setObject:[NSNumber numberWithInt:[pickerView selectedRowInComponent:1]] forKey:@"picker"];
    	[defaultsPicker synchronize];
    

    But I can't figure out how to restore it at launch. I tried:
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    	pickerView = [defaultsPicker objectForKey:@"picker"] ;
    

    but it isn't working.

    Thanks for any help in advance.
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • exorcyzeexorcyze Posts: 496Registered Users
    edited February 2009
    Try this?
    int selIndex = [defaultsPicker integerForKey:@"picker"];
    [pickerView selectRow:selIndex inComponent:0 animated:YES];
    
    My Apps on AppStore : <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301046713" target="_blank">gScale</a> (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    Thanks for the reply. Could you explain where I need to put the code?

    Thanks
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • exorcyzeexorcyze Posts: 496Registered Users
    edited February 2009
    Heh, well that depends largely on how you have things structured. You could put that in your viewDidLoad for the controller, or you could put it in the init (or initWithFrame ) of the view that has the pickerView on it, or you could get the value in your delegate and pass it to the view controller when that's instantiated. There's a buncha ways it could be approached. =)
    My Apps on AppStore : <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301046713" target="_blank">gScale</a> (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    Sorry, I should've explained better. I'm putting the code in applicationDidFinishLoading. I put your code like this:
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    	int selIndex = [[defaultsPicker objectForKey:@"picker"] intValue];
    

    But how do I set the UIPickerView selected row to selIndex? I tried puting:
    [pickerView selectedRowInComponent:selIndex] ;
    

    below your code but I don't think my last part is right and it doesn't seem to be working.

    Thanks exorcyze
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • exorcyzeexorcyze Posts: 496Registered Users
    edited February 2009
    Well, for setting you need to use pickerView selectRow:inComponent:animated: like in the code sample I posted above. The caveat is where is pickerView defined? It would seem from your first example that *gets* the value, your delegate has access to it in applicationWillTerminate? If so, then you should have access to it in the delegate when the app has finished launching ( and your controllers / controls etc have been created ).

    Make sense? =)
    My Apps on AppStore : <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301046713" target="_blank">gScale</a> (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    OK I'm starting to understand (sorry it's my first time using the control and I'm quite new to iPhone dev). I think the problem may be the way me pickerview is set up. It is declared in the app delegate.h file but the data source and it's delegate and connected to a separate settingsviewcontroller and that is where the pickerview methods are defined.

    I can see that I'm nearly there now thanks to you are selIndex holds the index of the row that needs to be selected. Now of course I just need to put in the code that selects it. Will I have to move the picker methods over the the appdelegate as this needs to be set when the application opens?

    Thanks for your help and explaining!
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • exorcyzeexorcyze Posts: 496Registered Users
    edited February 2009
    I would personally keep the delegate methods in the controller that is responsible for the view the picker is on, and then just call into that controller from the delegate to set the index to the given item on load. I like to keep my app delegate as uncluttered as possible, since things can get messy fast. =)
    My Apps on AppStore : <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301046713" target="_blank">gScale</a> (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    Yeh I try to keep the app delegate pretty tidy too. OK so I understand now and I've been trying to do that but I'm obviously not doing it correctly.

    Would something like this work?
    [pickerView selectedRowInComponent:
    

    although I'm not sure what to do after that.


    Thanks
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    I used
    [pickerView selectedRowInComponent:selIndex];
    

    and I think that will work. But not there is a problem with my code that saves the preference.
    NSUserDefaults *defaultsPicker = [NSUserDefaults standardUserDefaults];
    	[defaultsPicker setObject:[NSNumber numberWithInt:[pickerView selectedRowInComponent: ]] forKey:@"picker"];
    	[defaultsPicker synchronize];
    

    I don't know what to put after selectedRowInComponent
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • exorcyzeexorcyze Posts: 496Registered Users
    edited February 2009
    Pickers can have more than one spinner - each is considered a component. I believe it's a 0-based array, so if you only have one then you want to pass 0 there:
    [pickerView selectedRowInComponent:0]
    

    Once you grab that, try doing an NSLog on the value ( or set a breakpoint and step through ) to make sure you're getting what you think you should be. =)
    My Apps on AppStore : <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=301046713" target="_blank">gScale</a> (guitar scales reference), <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=302865690" target=
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited February 2009
    exorcyze wrote: »
    Pickers can have more than one spinner - each is considered a component. I believe it's a 0-based array, so if you only have one then you want to pass 0 there:
    [pickerView selectedRowInComponent:0]
    

    Once you grab that, try doing an NSLog on the value ( or set a breakpoint and step through ) to make sure you're getting what you think you should be. =)

    Thanks for the help exorcyze I got it working now :)
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • jaxjasonjaxjason Posts: 6New Users
    edited June 2009
    kieran12, where did you end up putting the code to pick the row in the uipicker. I have the same exact thing right now. I want it to set the row when the iser opens the program. Mine is on the main screen, always shown as the main user input.

    I create the arrays that populate my picker in the viewDidload function. So I know thats not tha place to put it (I think). Since that would imply that the picker hasnt asked any of the questions regarding numer of wheels, and # of rows per row.

    Any help would be greatly appreciated,
    Jason
    · ·
  • kieran12kieran12 Posts: 472Registered Users @ @
    edited June 2009
    jaxjason wrote: »
    kieran12, where did you end up putting the code to pick the row in the uipicker. I have the same exact thing right now. I want it to set the row when the iser opens the program. Mine is on the main screen, always shown as the main user input.

    I create the arrays that populate my picker in the viewDidload function. So I know thats not tha place to put it (I think). Since that would imply that the picker hasnt asked any of the questions regarding numer of wheels, and # of rows per row.

    Any help would be greatly appreciated,
    Jason

    Unfortunately I don't have the code anymore as I deleted the project but as far as I remember I put most of the code in the applicationDidFinishLaunching in my application delegate. I think saved the selected row number in to a variable and as I had done this in the app delegate it could be accessed by other classes. When the application was closed I saved the selected row using NSUserDefaults.
    <a href="http://itunes.apple.com/gb/artist/kieran-mcgrady/id301040994" target="_blank">On the iOS App Store</a>
    · ·
  • snorkelersnorkeler Posts: 2New Users
    edited May 2011
    There is a simple answer:

    If you do this in

    -(void) viewWillAppear: (BOOL) animated {
    [yourPickerViewName selectRow:yourRow inComponent:yourComponent animated:YES]
    }

    it will work. If you do it in viewWillLoad, it does not work.
    · ·
Sign In or Register to comment.