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 07-06-2009, 11:03 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 19
Default User Defaults question - so confused!

More newbie questions... I have several bool functions that if selected change the sate of a button. Here is some of the code:
if (checkboxSelected == 0){
[checkboxButton setSelected:YES];
} else {
checkboxSelected = 1;
[checkboxButton setSelected:NO];
}
}

I want a user to be able to save the state of the bool and the corresponding state of the button that is affected by the bool. I am also using the NSUserDefaults to save information from textFields elsewhere in my app using this type of code:
//Saving the default settings
NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
[Prefs setObject:inputField.text forKey:@"savedData"];
defaultSettings.text = @"Saved!";
//Loading the default settings
NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
NSString *Defaults = [Prefs stringForKey:@"savedData"];
if(Defaults == nil) {
defaultSettings.text = @"";
} else {
inputField.text = [[NSString alloc] initWithFormat:@"%@", Defaults];
}

That works great, but I have been unable to figure out the code for the bool functions. Can anyone point me to a tutorial or help clarify the code necessary? I've been Apple's dev center but now am even more confused.

Thanks in advance for your help.
aaronsbush is offline   Reply With Quote
Old 07-09-2009, 02:36 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 30
Default

So If I'm reading your question right you want to know how to pull the BOOL value from the NSUserDefaults. Since NSUserDefaults is nothing more than a dictionary these should work...

NSUserDefaults *Prefs = [NSUserDefaults standardUserDefaults];
// This will set it the defaults value
[prefs setBool:YES forKey:@"myCheckBoxValue"];

// This will get the BOOL value and set the checkboxButton BOOL
[checkboxButton setSelected:[prefs boolForKey:@"myCheckBoxValue"]];


I hope this helps...
sunfire is offline   Reply With Quote
Old 07-19-2009, 06:41 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 19
Default

Thanks for the help. This led me to the some code that worked for my app. Here's what I had to do in case anyone runs into a similar issue:

Controller.h
#define Defaults @"defaultSettings"

Controller.m
- (void)viewDidLoad {

NSUserDefaults *defaultPrefs = [NSUserDefaults standardUserDefaults];
checkboxButton.selected = ([[defaultPrefs objectForKey:Defaults] isEqualToString:@"checkboxSelectedYes"]) ? YES : NO;
if (checkboxButton.selected) {
[self checkboxButtonIBaction:checkboxButton];
}
}

- (IBAction)defaultIBaction:(id)sender {
NSUserDefaults *defaultPrefs = [NSUserDefaults standardUserDefaults];
NSString *setDefaults = (checkboxButton.selected) ? @"checkboxSelectedYes" : @"checkboxSelectedNo";
[defaultPrefs setObject:setDefaults forKey:Defaults];
}

I found this code in Chapter 10 of Beginning iPhone Development. I'm not sure if this is the best way but it works.
aaronsbush is offline   Reply With Quote
Reply

Bookmarks

Tags
defaults, nsuserdefaults, preferences

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: 347
19 members and 328 guests
ADY, Dani77, Duncan C, F_Bryant, Grinarn, HemiMG, Herbie, jansan, JasonR, linkmx, macquitzon216, mer10, Monstertaco, prchn4christ, Robiwan, sly24, Touchmint, twerner, zulfishah
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,760
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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