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-13-2009, 04:35 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Exclamation Save Color With NSUserDefaults?

Hi,
I have been trying to figure out how to change the color of a UILabel and then save into the NSUserDefaults. I am using the Settings.bundle to change all of the settings. Here is the code I used...
Code:
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	label.text = [defaults objectForKey:kColorKey];
That code works, but it changes the text not the text color. Can someone help me?

Thanks
- Dan
dsinc14 is offline   Reply With Quote
Old 12-13-2009, 04:37 PM   #2 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
Default

Quote:
Originally Posted by dsinc14 View Post
Hi,
I have been trying to figure out how to change the color of a UILabel and then save into the NSUserDefaults. I am using the Settings.bundle to change all of the settings. Here is the code I used...
Code:
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	label.text = [defaults objectForKey:kColorKey];
That code works, but it changes the text not the text color. Can someone help me?

Thanks
- Dan
why would it change the colot if you said label.text all you are doing is saving the text. I dont get how you think that would save the color.
justill45 is offline   Reply With Quote
Old 12-13-2009, 04:38 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Default

Quote:
Originally Posted by justill45 View Post
why would it change the colot if you said label.text all you are doing is saving the text. I dont get how you think that would save the color.
Yeah, I know it does not work. I have trying changing that to many different things, but none of them worked. So, I just wanted to show that code I had to work with!
dsinc14 is offline   Reply With Quote
Old 12-13-2009, 04:43 PM   #4 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
Default

Quote:
Originally Posted by dsinc14 View Post
Yeah, I know it does not work. I have trying changing that to many different things, but none of them worked. So, I just wanted to show that code I had to work with!
always look in the documentation before posting. It says theres a propert of UILabel called textColor.

so its yourLabel.textColor
justill45 is offline   Reply With Quote
Old 12-13-2009, 04:45 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Default

Quote:
Originally Posted by justill45 View Post
always look in the documentation before posting. It says theres a propert of UILabel called textColor.

so its yourLabel.textColor
I tried that and it didn't work. The app crashed on startup!
dsinc14 is offline   Reply With Quote
Old 12-13-2009, 06:24 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
Default

To save a colour object, you first have to archive it into a data object. The "User Defaults Programming Topics for Cocoa" describes how to do this (although for an NSColor object, but since they behave in the same way...).

Warning: I haven't tried running this yet, and it's based on Apple's example code only altered to use an NSKeyedArchiver, so it may include errors, or may just simply blow up, but hopefully it should set you off in the right direction.

To save the colour:
Code:
// first get the colour you want to save
UIColor *theColour = label.textColor;
// archive it into a data object
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: theColour];
// write the data into the user defaults
[[NSUserDefaults standardUserDefaults] setObject: data forKey: kColorKey];
And then to recover it:
Code:
// declare the colour var you'll be needing
UIColor *theColour;
// read the data back from the user defaults
NSData *data= [[NSUserDefaults standardUserDefaults] dataForKey: kColorKey];
// check whether you got anything
if(data == nil) {
  // use this to set the colour the first time your app runs
  theColour = [UIColor someColor];
} else {
  // this recreates the colour you saved
  theColour = (NSColor *)[NSKeyedUnarchiver unarchiveObjectWithData: data];
}
// and finally set the colour of your label
label.textColor = theColour;
__________________
SimCap - Simple iPhone and iPad Simulator screen capture

Last edited by _sjc_; 12-13-2009 at 06:30 PM.
_sjc_ is offline   Reply With Quote
Reply

Bookmarks

Tags
color, nsuserdefaults, settings.bundle

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: 263
20 members and 243 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, diyora, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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