• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Where to set default user defaults?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where to set default user defaults?


  • Subject: Re: Where to set default user defaults?
  • From: Jens Bauer <email@hidden>
  • Date: Fri, 21 Nov 2003 19:41:04 +0100

Hi John,

On Friday, November 21, 2003, at 02:38 PM, John Timmer wrote:

I know there's been some discussion in the past about what the order of
"wakeFromNib" calling is (and the answer seems to be "none is guaranteed").
A related question that I couldn't find in the archives - are all the "init"
methods called before all the "awakeFromNib" methods?

The reason I'm asking is that I want to set some default user defaults for
the first time the app launches, and I'm going to need some of them before
the application's interface gets displayed. So, in short, I need to have
something that's called first during the app launching process.

I have another solution, which I find very neat.

Create yourself a Preference class. Make a shared instance.
This Preference object will never be released, not even if you try doing a [instance release] on it. (And that comes from me, who JUST TOLD people to not leak memory!)
-But the good thing about this, is that there can be only one.. Eg. If you try allocating another instance, you'll get NULL. Always access it through..
[Preferences sharedInstance]

Eg.
if([[Preferences sharedInstance] showNameColumn])
{
// ...show that column or enable it or whatever...
}

The very first time this shared instance is used, the Preference class will read the preferences from disk.

Now, in the readFromDisk method, you first initalize all members to hold default values.
After that, you read the NSUserDefaults, and grabs the objectForKey.. If it's NULL, don't do anything, otherwise read the intValue, floatValue, stringValue or whatever. :)

The above should be enough for you to implement it, but to get you there a bit faster, I'd like to include some stuff here:

/* Preferences.h */

@interface Preferences : NSObject
{
IBOutlet id window;

BOOL showNameColumn;
// ...
// other preference values.
// ...

NSUserDefaults *defaults;
}
+ (Preferences *)sharedInstance;

- (id)init;
- (void)dealloc;
- (IBAction)apply:(id)sender;
- (IBAction)cancel:(id)sender;
- (IBAction)ok:(id)sender;
- (IBAction)openPanel:(id)sender;
- (IBAction)revert:(id)sender;
- (IBAction)revertToFactorySettings:(id)sender;
- (IBAction)save:(id)sender;

+ (void)openPanel;
+ (void)readFromDisk;
+ (void)writeToDisk;

- (void)readPreferencesFromDisk;
- (void)writePreferencesToDisk;

// Accessor methods:
- (void)setShowNameColumn:(BOOL)aShowNameColumn;
- (BOOL)showNameColumn;
@end

// Preferences.m
#import "Preferences.h"

@implementation Preferences

static Preferences *sharedInstance = NULL;

+ (Preferences *)sharedInstance
{
return(sharedInstance ? sharedInstance : [[self alloc] init]);
}

- (id)init
{
if(sharedInstance)
{
[self release];
}
else
{
self = [super init];
if(self)
{
sharedInstance = self;
[self readPreferencesFromDisk];
}
}
return(sharedInstance);
}

- (void)dealloc
{
if(self != sharedInstance)
{
[super dealloc]; // Don't free the shared instance
}
}

+ (void)readFromDisk
{
[Preferences sharedInstance]; // This is enough!
}

+ (void)writeToDisk
{
[[Preferences sharedInstance] writePreferencesToDisk];
}

+ (void)openPanel
{
[[Preferences sharedInstance] openPanel:self];
}

// ... a bunch of Action methods {snipped}

- (void)readPreferencesFromDisk
{
NSObject *value;

// Set up default values:
showNameColumn = YES;

// Now look on the disk, and see if we can find some values that override the defaults we've set up already...
defaults = [NSUserDefaults standardUserDefaults];
value = [defaults objectForKey:@"Show Name Column"];
if(value)
{
showNameColumn = [value boolValue];
}
}

- (void)writePreferencesToDisk
{
[self writeShowNameColumn];
}

- (void)setShowNameColumn:(BOOL)aShowNameColumn
{
showNameColumn = aShowNameColumn;
}

- (BOOL)showNameColumn
{
return(showNameColumn);
}

@end


This source was made up inside Mail.app (copied and pasted from my own sources and modified), so it hasn't been compiled, and may contain errors. I hope you can use it anyway. :)



Love,
Jens
_______________________________________________
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.

  • Follow-Ups:
    • Re: Where to set default user defaults?
      • From: mmalcolm crawford <email@hidden>
    • Re: Where to set default user defaults?
      • From: Jens Bauer <email@hidden>
References: 
 >Where to set default user defaults? (From: John Timmer <email@hidden>)

  • Prev by Date: Re: Problem Ejecting Discs
  • Next by Date: Re: Where to set default user defaults?
  • Previous by thread: Re: Where to set default user defaults?
  • Next by thread: Re: Where to set default user defaults?
  • Index(es):
    • Date
    • Thread