• 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: Speaking of NSUserDefaults..(newbie warning)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Speaking of NSUserDefaults..(newbie warning)


  • Subject: Re: Speaking of NSUserDefaults..(newbie warning)
  • From: Chris Parker <email@hidden>
  • Date: Mon, 3 Dec 2001 15:51:21 -0800

Hi Mark,

The strategy here is something like this:

You're going to need a window to display your text fields, which pretty much implies that you're going to be creating a subclass of NSWindowController - maybe something called "PreferencesController". This is probably best set up in Interface Builder as a separate nib, with a "File's Owner" of your PreferencesController class.

The PreferencesController class should probably have outlets for each of the values you're interested in setting. When you first create the window, you should populate the fields with their values. When you are told that the window is about to go away, get the values for these fields and write them to the NSUserDefaults standard defaults object and synchronize.

Let's say you've got two fields - their outlets are textField1 and textField2, and they're defined to contain integers (let's be optimistic here for a second and assume they're integers by the time we're ready to look at their values).

Getting the default values is pretty straightforward:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int default1FromDisk = [defaults integerForKey:@"Value1"];
int default2FromDisk = [defaults integerForKey:@"Value2"];
[textField1 setIntValue:default1FromDisk];
[textField2 setIntValue:default2FromDisk];

When you get ready to leave the window, do the reverse:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:[textField1 intValue] forKey:@"Value1"];
[defaults setInteger:[textField2 intValue] forKey:@"Value2"];
[defaults synchronize];

Although that last synchronize is technically unnecessary - the sync will occur when your application quits, so you don't have to do it when your window closes.

.chris

On Monday, December 3, 2001, at 02:44 PM, Mark Wridt wrote:

I am trying to make a simple preferences window for my
application. I have looked at Mike Beam's O'Rielly
page and I am just not far enough along in Cocoa and
coding in general to figure out how to implement some
of this code. I have also checked the Developer's
example in the Appkit called NSUserDefaults, but it is
not functioning properly for me. SO after much
struggle , I am turning to the List.

I have some TextFields in a preferences window.
Integers placed in them are used throughout the
application. I wish to have the ability for the User
to change the values of the integers and they remain
that way until they are changed again, even if the
application is quit and relaunched. It seems SO
simple and similar to the examples, yet I am at a
loss.

Help?

--
Chris Parker <email@hidden>
Cocoa Frameworks Engineer


References: 
 >Speaking of NSUserDefaults..(newbie warning) (From: Mark Wridt <email@hidden>)

  • Prev by Date: create a image from a NSString or NSView.
  • Next by Date: Re: create a image from a NSString or NSView.
  • Previous by thread: Speaking of NSUserDefaults..(newbie warning)
  • Next by thread: create a image from a NSString or NSView.
  • Index(es):
    • Date
    • Thread