来源: ” target=”_blank”>http://iphonedevelopertips.com/cocoa/read-and-write-user-preferences.html
这段代码太有用了,非常感谢原作者
//
// SetPrefsAppDelegate.h
//
#import <UIKit/UIKit.h>
@interface SetPrefsAppDelegate : NSObject <UIApplicationDelegate>
{
BOOL saveUsername;
NSInteger preferredIndexInTabbar;
UIWindow *window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
// SetPrefsAppDelegate.m
//
#import "SetPrefsAppDelegate.h"
#import "Preferences.h"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// App Delegate implementation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@implementation SetPrefsAppDelegate
@synthesize window;
/*------------------------------------------------------
* Read preferences from system
*-----------------------------------------------------*/
-(void) loadPreferences
{
saveUsername = [Preferences shouldSaveUsername];
preferredIndexInTabbar = [Preferences startupTab];
}
/*------------------------------------------------------
* Application startup code
*-----------------------------------------------------*/
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// Load user preferences (notice the default values the first time through)
[self loadPreferences];
// Show the current values of the preferences
NSLog(@"Save user preferences: %s", saveUsername == YES ? "Yes" : "No");
NSLog(@"Preferred startup tab: %d", preferredIndexInTabbar);
// This is a little contrived, but you get the point...
BOOL saveUname = YES;
NSInteger index = 3;
// Write new values to the sytem
[Preferences setPreferences:saveUname startupTab:index];
// NSLog(@"Home: %s", NSHomeDirectory());
// NSLog(@"Home: %@", NSHomeDirectory());
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc
{
[window release];
[super dealloc];
}
@end