Re: Saving is so darn hard!
Re: Saving is so darn hard!
- Subject: Re: Saving is so darn hard!
- From: Charles Srstka <email@hidden>
- Date: Sun, 31 Mar 2002 16:36:41 -0600
On Sunday, March 31, 2002, at 04:20 PM, deekpyro wrote:
What I want to do is have it save the state of the checkbox so the next
time
the app starts up it is at the state that it was when it was last
closed.
Any ideas?
Um, do exactly what you were doing, except don't set the preference to
YES every time the application loads. In other words, leave out what you
were doing in the initialize method, and just do the stuff in the
windowDidLoad and the check box action.
One other thing is to make sure the windowDidLoad method actually loads
when the program starts. I could be wrong, but I think that
windowDidLoad is a method of NSWindowController that you can only
override if you are a subclass of that class. You might want to try
awakeFromNib or applicationDidFinishLaunching: instead.
PS. I also want to know how to copy files in Cocoa
Thanks
on 3/31/02 4:05 PM, Charles Srstka at email@hidden wrote:
I dunno, it just looks to me like your initialize method is setting the
preference to YES every time you start up. I just get [NSUserDefaults
standardUserDefaults] and get the preference from it without worrying
about sending a dictionary to registerDefaults:. If what you're trying
to do is have some default settings that are used the first time the
app
is run, you might want to try something like this:
- (NSDictionary *)defaultValues
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],STSCheckBoxKey,
/*the rest of your default settings go here*/,
nil];
}
- (id)getPreferenceForKey:(NSString *)key
{
id thePref;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (!(thePref = [prefs objectForKey:key]))
{
thePref = [[self defaultValues] objectForKey:key];
[prefs setObject:thePref forKey:key];
}
return thePref;
}
Then call [[self getPreferenceForKey:STSCheckBoxKey] boolValue] in your
code to get the state you need to set the check box to.
Charles
On Sunday, March 31, 2002, at 03:53 PM, deekpyro wrote:
Like what? (this is pretty much what my book told me to put for
+initialize
Thanks
on 3/31/02 3:17 PM, Charles Srstka at email@hidden wrote:
How about commenting out the stuff that's in +(void)initialize?
On Sunday, March 31, 2002, at 03:00 PM, deekpyro wrote:
Ok, I think I've got most of it, but it still doesn't work:
#import "Controller.h"
NSString *STSCheckBoxKey = @"WBC Enabled";
@implementation Controller
+ (void)initialize
{
// Create a dictionary
NSMutableDictionary *defaultValues = [NSMutableDictionary
dictionary];
// Put the values in the dictionary
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:STSCheckBoxKey];
// Register the dictionary of defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:
defaultValues];
NSLog(@"registered default: %@", defaultValues);
}
- (void)windowDidLoad
{
NSUserDefaults *defaults;
defaults = [NSUserDefaults standardUserDefaults];
[theCheckBoxWBC setState:[defaults boolForKey:STSCheckBoxKey]];
}
// actions //
- (IBAction)theCheckBoxWBCClicked:(id)sender
{
[[NSUserDefaults standardUserDefaults] setBool:[sender state]
forKey:
STSCheckBoxKey];
}
What do you think?
on 3/31/02 1:11 PM, Charles Srstka at email@hidden wrote:
Read the documentation for NSUserDefaults and you will soon be
saying,
"Saving is so darn easy!"
On Sunday, March 31, 2002, at 11:27 AM, deekpyro wrote:
I want to set up my program so that when it loads whatever the
value
of
theCheckBox would be the same as when you closed it (hence it
saves).
I've
been refering to multiple books but all of them just confuse
me. I
know
about the NSCoding protocal and to use NSArchiver and
NSUnarchiver,
but
those are the ones that get me. I know how to do encodeWithCoder
and
initWithCoder but those NSArchivers and NSUnarchivers get me.
Could
someone
PLEASE show me how to implement those for saving a simple
checkBox?
Thanks
so much!
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.