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 11-20-2009, 12:54 AM   #1 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 23
Question save data with multiple UISwitch

I have some UISwitch on my app's setting page. I'd like to save state of these UISwitch and want them to remain same state as before after relaunch app. How do you do it? Should I save it into .plist file by using stringByAppendingPathComponent method? or Should I use NSUserDefaults method?
Please help!
k2c is offline   Reply With Quote
Old 11-21-2009, 12:34 PM   #2 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 23
Unhappy

anybody?
k2c is offline   Reply With Quote
Old 11-21-2009, 12:50 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

I'd suggest saving them into the user defaults, since it's the more logical location (and also less work for you).

Come up with a unique setting name for each switch, and then save them in -applicationWillTerminate and restore them in -applicationDidFinishLaunching, eg.

Code:
-(void)applicationDidFinishLaunching:(UIApplication *)app {

    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

    mySwitchOne.on = [defs boolForKey: @"mySwitchOneSetting"];
    mySwitchTwo.on = [defs boolForKey: @"mySwitchTwoSetting"];
}

-(void)applicationWillTerminate:(UIApplication *)app {

    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

    [defs setBool: mySwitchOne.on forKey: @"mySwitchOneSetting"];
    [defs setBool: mySwitchTwo.on forKey: @"mySwitchTwoSetting"];
}
You may also want to look at the -registerDefaults: method of NSUserDefaults for how to set up the default values of the switches for the first time the application runs.
__________________
SimCap - Simple iPhone and iPad Simulator screen capture
_sjc_ is offline   Reply With Quote
Old 11-21-2009, 01:49 PM   #4 (permalink)
k2c
Registered Member
 
Join Date: Oct 2009
Posts: 23
Question

Thank you very much for spending time to answer my question, _sjc_!
I just tried the code you suggested, but it didn't save previous status of switches. Did I miss something?

.m file
Code:
-(void)applicationDidFinishLaunching:(UIApplication *)app {
	
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	
    SwitchOne.on = [defs boolForKey: @"MYSwitchOne"];
    SwitchTwo.on = [defs boolForKey: @"MYSwitchTwo"];
}

-(void)applicationWillTerminate:(UIApplication *)app {
	
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	
    [defs setBool: SwitchOne.on forKey: @"MYSwitchOne"];
    [defs setBool: SwitchTwo.on forKey: @"MYSwitchTwo"];
}

.h file
Code:
-(void)applicationDidFinishLaunching:(UIApplication *)app;
-(void)applicationWillTerminate:(UIApplication *)app;
k2c is offline   Reply With Quote
Old 01-19-2010, 06:41 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 12
Default

I doubt you still care about this, but I think I know whats wrong. You have to add in a few lines to deal with the applicationWillTerminate part.

Code:
-(void)applicationDidFinishLaunching:(UIApplication *)app {
	
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	
    SwitchOne.on = [defs boolForKey: @"MYSwitchOne"];
    SwitchTwo.on = [defs boolForKey: @"MYSwitchTwo"];

//for activating the state saver later
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
}

-(void)applicationWillTerminate:(UIApplication *)app {
	
    NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
	
    [defs setBool: SwitchOne.on forKey: @"MYSwitchOne"];
    [defs setBool: SwitchTwo.on forKey: @"MYSwitchTwo"];

    [[NSUserDefaults standardUserDefaults] synchronize];


}
gerakis is offline   Reply With Quote
Reply

Bookmarks

Tags
data, multiple, nsuserdefaults, save, uiswitch

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: 267
16 members and 251 guests
@sandris, ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, HemiMG, jansan, JasonR, MarkC, prchn4christ, ryandb2, smethorst, tomtom100
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
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:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0