Cocoa:Simple NSUserDefaults example

Cocoa:Simple NSUserDefaults example

//

//  AppController.h

//  SimpleUserDefaults

//

//  Created by Biao Huang on 12/22/11.

//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.

//


#import <Foundation/Foundation.h>


extern NSString * const DefaultsFavoriteBookKey;

extern NSString * const DefaultsFavoriteCityKey;

extern NSString * const DefaultsFavoriteFoodKey;

extern NSString * const DefaultsFavoriteColorKey;


@interface AppController : NSObject {

    IBOutlet NSTextField *bookField;

    IBOutlet NSTextField *cityField;

    IBOutlet NSTextField *foodField;

    IBOutlet NSTextField *colorField;

    

    NSUserDefaults *preferences;

}


- (IBAction)textFieldChanged:(id)sender;

@end


//

//  AppController.m

//  SimpleUserDefaults

//

//  Created by Biao Huang on 12/22/11.

//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.

//


#import "AppController.h"


NSString * const DefaultsFavoriteBookKey = @"Favorite Book";

NSString * const DefaultsFavoriteCityKey = @"Favorite City";

NSString * const DefaultsFavoriteFoodKey = @"Favorite Food";

NSString * const DefaultsFavoriteColorKey = @"Favorite Color";


@implementation AppController


+ (void)initialize {

    if (self != [AppController class]) {

        return;

    }

    

    NSMutableDictionary *defaultPrefs = [NSMutableDictionary dictionary];

    [defaultPrefs setObject:@"Cocoa Programming" forKey:DefaultsFavoriteBookKey];

    [defaultPrefs setObject:@"Braunschweig" forKey:DefaultsFavoriteCityKey];

    [defaultPrefs setObject:@"Fleisch" forKey:DefaultsFavoriteFoodKey];

    [defaultPrefs setObject:@"Schwarz" forKey:DefaultsFavoriteColorKey];

    

    // Regisgter factory preferences.

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultPrefs];

}


- (id)init {

    self = [super init];

    if (self) {        

        preferences = [[NSUserDefaults standardUserDefaults] retain];

    }

    return self;

}


- (void)dealloc {

    [preferences release];

    [super dealloc];

}


- (void)awakeFromNib {

    [bookField setStringValue:[preferences stringForKey:DefaultsFavoriteBookKey]];

    [cityField setStringValue:[preferences stringForKey:DefaultsFavoriteCityKey]];

    [foodField setStringValue:[preferences stringForKey:DefaultsFavoriteFoodKey]];

    [colorField setStringValue:[preferences stringForKey:DefaultsFavoriteColorKey]];    

}


- (IBAction)textFieldChanged:(id)sender {

    NSString *favorite = [sender stringValue];

    

    if (sender == bookField) {

        [preferences setObject:favorite forKey:DefaultsFavoriteBookKey];

    } else if (sender == cityField) {

        [preferences setObject:favorite forKey:DefaultsFavoriteCityKey];

    } else if (sender == foodField) {

        [preferences setObject:favorite forKey:DefaultsFavoriteFoodKey];

    } else if (sender == colorField) {

        [preferences setObject:favorite forKey:DefaultsFavoriteColorKey];

    }

}

@end


@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

你可能感兴趣的:(Cocoa:Simple NSUserDefaults example)