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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-13-2009, 05:24 PM   #6 (permalink)
_sjc_
Registered Member
 
Join Date: Nov 2009
Location: London
Posts: 226
_sjc_ is on a distinguished road
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 05:30 PM.
_sjc_ is offline   Reply With Quote
 

» Advertisements
» Online Users: 848
19 members and 829 guests
ADY, Ananth, anil_shanebond, AyClass, baja_yu, brajpal, Christioren, erdinc27, givensur, HemiMG, HenryCLacey, ilmman, lzwasyc, momolgtm, Nobbsy, pipposanta, Renrob, RockBaby, SillyHoney
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,389
Threads: 94,003
Posts: 402,462
Top Poster: BrianSlick (7,978)
Welcome to our newest member, HenryCLacey
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:06 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.