 |
 |
NSUserDefaults in java?
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Location: Ronneby, Sweden
Status:
Offline
|
|
Can someone point me to an example of using the NSUserDefaults class with java. I don't understand how to create a domain with it. From the docs on apple webpage it seams like the name of the domain is the Bundle Identifier, what is that and how do I change it?
Thanks,
Gabbe
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Assuming you want to store per-user settings for your app, this is very easy.
The bundle identifier is set in the CFBundleIdentifier key in the Info.plist file, which is part of your app bundle package. This is most easily set through ProjectBUilder's "Application Settings" pane (when inspecting the target). You will want to choose something unique --- use the java package name style, something like com.companyname.appname.
Note that if you don't specify this, things should work by default. The app name will be used as the domain name. However, this might increase the chance of conflicts.
The Game.java class in the BlastApp example (/Developer/Examples/Java/AppKit/BlastApp) uses NSUserDefaults to store preferences and highscore settings.
Ali
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Location: Ronneby, Sweden
Status:
Offline
|
|
Thanks, that really helped. So now I know how to write and read preferences. Now I have another pretty fundamental question, how do I "catch" events? Let's say I want to save my preferences when the user quits my application or that I want to quit my application when the user closes the main window, how do I do that?
I think it would be pretty logical that the window had a close-outlet or something. Where do I find these events?
/Gabbe
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Originally posted by gabrielf:
Thanks, that really helped. So now I know how to write and read preferences. Now I have another pretty fundamental question, how do I "catch" events? Let's say I want to save my preferences when the user quits my application or that I want to quit my application when the user closes the main window, how do I do that?
I think it would be pretty logical that the window had a close-outlet or something. Where do I find these events?
/Gabbe
The "events" your describing are available in many cases as "delegate" methods. Some objects (for instance, NSWindow or NSApplication) have delegates, and on certain important events, they consult or notify their delegates. Typically these are things that can be accomplished by subclassing, but often another object is better suited for the task. For instance, when a window is being closed, it makes sense that the window asks the document object (that is, the object which holds state of the document) whether it's OK to close or not.
Note that in addition to delegation, there is also "notification". Delegates are usually consulted for info, and the delegate might intervene in the process (for instance, the delegate could prevent a window from being closed). Notifications are like news reports; they allow multiple observers to listen for changes without directly influencing the outcome. Rather than intervening in the process, observers usually do some additional work upon hearing that something's going down --- for instance, once a window asks its delegate whether it's OK to close down, and the delegate responds favorably, the window then sends out a notification that it's closing. Any observer registered to hear this will get the notification.
Use setDelegate: to set the delegate of an object. See "delegate" methods in header files or documentation (For instance, NSwindow delegate methods are in NSWindow.h or with NSWindow documentation). Use the NSNotificationCenter class to register/unregister observers; again see the header of a given class to see what notifications that class sends out.
BTW, the two specific things you want to do:
- save preferences on quit: this is automatically done for you in Cocoa apps
- quit app when last window closed: this will also be automatically done if you implement a NSApplication delegate method to return YES
Ali
[This message has been edited by ali (edited 10-30-2000).]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|