逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

//

//  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);
posted on 2011-12-22 23:33 逛奔的蜗牛 阅读(840) 评论(5)  编辑 收藏 引用 所属分类: Cocoa

评论

# re: Cocoa:Simple NSUserDefaults example 2014-04-14 10:00 Friv
谢谢编码  回复  更多评论
  

# re: Cocoa:Simple NSUserDefaults example 2014-07-19 11:45 Kizi 5
很不错的信息后,你已经共享,感谢您的工作的信息共享。  回复  更多评论
  

# re: Cocoa:Simple NSUserDefaults example 2014-08-12 09:16 friv
非常好,内容丰富的文章实在。它帮助了我很多,以提高我的知识,我真的很喜欢的作家提出了他的看法的方式。我希望在将来能看到更多的信息和有用的文章。  回复  更多评论
  

# re: Cocoa:Simple NSUserDefaults example 2014-12-23 21:07 friv
thank your blog   回复  更多评论
  

# re: Cocoa:Simple NSUserDefaults example 2015-01-06 22:41 jogos de friv
I follow you and have been  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理