Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.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 12-21-2008, 07:31 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default NSUserDefaults with UISegmentedControl

Hey everyone,

I have a UISegmentedControl with three segments. I also have a button/IBAction called updatePrefs. When this is clicked I want to save the NSUserDefaults. It needs to remember which segment the user has selected. I have tried this code:

Code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[feedProvider selectedSegmentIndex] forKey:@"feedProvider"];
	[defaults synchronize];
(feedProvider is the name of the UISegmentedControl)

I get an error:

passing argument setObject:forKey makes pointer from integer without cast.

Any ideas to what is wrong.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-21-2008, 12:08 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Denver
Posts: 172
Default Try this...

Your problem is that you are trying to set an int as an object. Try this instead assuming feedProvider is your UISegmentControl object.

Code:
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[NSNumber numberWithInt:[feedProvider selectedSegmentIndex]] forKey:@"feedProvider"];
	[defaults synchronize];
BSDimwit is offline   Reply With Quote
Old 12-21-2008, 02:25 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Default

great that seems to be working. How can i restore the NSUserDefaults?
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Old 12-21-2008, 02:46 PM   #4 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Denver
Posts: 172
Default Like so.

Code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
feedProvider.selectedSegmentIndex = [[defaults objectForKey:@"feedProvider"] intValue];
BSDimwit is offline   Reply With Quote
Old 12-21-2008, 02:49 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Smile thanks

Thanks a lot BSDimwit that worked great.
__________________
On the iOS App Store
kieran12 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsuserdefaults, uisegmentedcontrol

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



» Advertisements
» Online Users: 239
20 members and 219 guests
ADY, AragornSG, BrianSlick, Dani77, Dattee, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister, viniciusdamone, vvenkatachallam
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:20 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0