Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-09-2009, 05:42 PM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 26
Default Save State of Picker

I am trying to save a picker row so that it displays the same selection the next time a user loads the app.
I'm not getting any warnings or errors, but the changes aren't saving. Any suggestions?

Code:
NSUserDefaults *wheelPrefs = [NSUserDefaults standardUserDefaults];
	wheelPicker = [wheelPrefs objectForKey:@"wheelData"];
	[wheelPrefs synchronize];
Code:
-(void)applicationDidTerminate {
	NSUserDefaults *wheelPrefs = [NSUserDefaults standardUserDefaults];
	[wheelPrefs setObject:[NSNumber numberWithInt:[wheelPicker selectedRowInComponent:0]] forKey:@"wheelData"];
}
Cheers
SimonCowell is offline   Reply With Quote
Old 06-09-2009, 05:52 PM   #2 (permalink)
Almost there
 
Join Date: Dec 2008
Location: Leeds, England
Posts: 629
Default

This is what I do
Code:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
	[prefs setInteger:row forKey:@"pickerRow"];
}
And then to load it up.
Code:
- (void)viewDidLoad {
  [super viewDidLoad];
  NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
  [pckrView selectRow:[prefs integerForKey:@"pickerRow"] inComponent:0 animated:YES];
}
Just make sure all your IB connections are right.
Tom.
harrytheshark is offline   Reply With Quote
Old 06-09-2009, 05:59 PM   #3 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Code:
	wheelPicker = [wheelPrefs objectForKey:@"wheelData"];
Gets an object from the preferences. You should have used:

Code:
        [wheelPrefs setObject: wheelPicker forKey:@"wheelData"];
But that's probably not going to work either, since you don't want to actually try to save the picker. You want to save the value of the picker, more like:

Code:
	[wheelPrefs setInteger: [wheelPicker row] forKey:@"pickerRow"];
joe
FlyingDiver is offline   Reply With Quote
Old 06-10-2009, 11:04 AM   #4 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 26
Default Multiple Pickers

Thank you both! This worked beautifully!
BUT, there's always something....
I have multiple pickers (they are stacked so they show and hide) and only the most recent picker change is saving. Any ideas?

Code:
NSUserDefaults * wheelPrefs = [NSUserDefaults standardUserDefaults];
	[wheelPicker selectRow:[wheelPrefs integerForKey:@"wheelPickerRow"] inComponent:0 animated:YES];
NSUserDefaults * carPrefs = [NSUserDefaults standardUserDefaults];
	[carPicker selectRow:[carPrefs integerForKey:@"carPickerRow"] inComponent:0 animated:YES];
Code:
NSUserDefaults * wheelPrefs = [NSUserDefaults standardUserDefaults];
NSUserDefaults * carPrefs = [NSUserDefaults standardUserDefaults];
Code:
[wheelPrefs setInteger:row forKey:@"wheelPickerRow"];
[carPrefs setInteger:row forKey:@"carPickerRow"];
SimonCowell is offline   Reply With Quote
Old 06-10-2009, 11:26 AM   #5 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

There's nothing obviously wrong with your code, other than this is redundant:

Code:
NSUserDefaults * wheelPrefs = [NSUserDefaults standardUserDefaults];
NSUserDefaults * carPrefs = [NSUserDefaults standardUserDefaults];
You only need one prefs pointer object, not one for each preference you're retrieving/saving.

From what little you're showing, it looks like you should be saving and restoring the current value for each of those two pickers. Is something else happening?

joe
FlyingDiver is offline   Reply With Quote
Old 06-10-2009, 11:45 AM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 26
Default

Thanks, I have gotten rid of the redundant line. I feel like this line
Code:
[prefs setInteger:row forKey:@"wheelPickerRow"];
needs something like this
Code:
[prefs setInteger:wheelPicker row forKey:@"wheelPickerRow"];
SimonCowell is offline   Reply With Quote
Old 06-10-2009, 11:53 AM   #7 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 26
Default

Quote:
Originally Posted by SimonCowell View Post
Thanks, I have gotten rid of the redundant line. I feel like this line
Code:
[prefs setInteger:row forKey:@"wheelPickerRow"];
needs something like this
Code:
[prefs setInteger:wheelPicker.row forKey:@"wheelPickerRow"];
The problem is that the last picker choice is setting the row number for all of the other pickers
SimonCowell is offline   Reply With Quote
Old 06-10-2009, 07:32 PM   #8 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 26
Default Solution

[prefs setObject:[NSNumber numberWithInt:[wheelPicker selectedRowInComponent:0]] forKey:@"wheelPickerRow"];
SimonCowell is offline   Reply With Quote
Old 06-10-2009, 07:32 PM   #9 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by SimonCowell View Post
The problem is that the last picker choice is setting the row number for all of the other pickers
Are you saving the row number for each picker as the user selects it? Or trying to save them all at the same time? Your code shows the same variable (row) used for each default, so I that would be the expected result.

joe
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,296
Threads: 39,092
Posts: 171,394
Top Poster: smasher (2,575)
Welcome to our newest member, Issue
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:08 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0