码迷,mamicode.com
首页 > 移动开发 > 详细

IOS问题汇总:2015-1-9 NSUserDefault (转)

时间:2015-04-15 09:34:02      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

NSUserDefault

NSUserDefaults可以存取一些短小的信息。

比如存入再读出一个字符串到NSUserDefaults:

[cpp] view plaincopy
NSString *string = [NSString stringWithString @“hahaha”];

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

[ud setObject:string forKey:@“myKey”];

NSString *value;

value = [ud objectForKey:“myKey”];

但是并不是所有的东西都能往里放的。NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.

我找到的方法是,让这个自定义类实现协议中的- (id) initWithCoder: (NSCoder *)coder方法和- (void) encodeWithCoder: (NSCoder *)coder方法(obj-c的协议protocol就是java的接口interface,就是C++的纯虚函数),然后把该自定义的类对象编码到NSData中,再从NSUserDefaults中进行读取。

[cpp] view plaincopy
@interface BusinessCard : NSObject {

NSString *_firstName;

NSString *_lastName;

}

@property (nonatomic, retain) NSString *_firstName;

@property (nonatomic, retain) NSString *_lastName;

@end;

@implementation BusinessCard

@synthesize _firstName, _lastName;

-(void)dealloc{
[_firstName release];
[_lastName release];
[super dealloc];
}

-(id) initWithCoder: (NSCoder *)coder
{
if (self = [super init])
{
self._firstName = [coder decodeObjectForKey:@“_firstName”];
self._lastName = [coder decodeObjectForKey:@“_lastName”];
}
return self;
}

-(void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:_firstName forKey:@“_firstName”];
[coder encodeObject:_lastName forKey:@“_lastName”];

}

@end

然后再存取时通过NSData做载体:

[cpp] view plaincopy
BusinessCard *bc = [[BusinessCard alloc] init];

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

NSData *udObject = [NSKeyedArchiver archivedDataWithRootObject:bc];

[ud setObject:udObject forKey:@“myBusinessCard”];

[bc release];

udObject = nil;

udObject = [ud objectForKey:@“myBusinessCard”];

bc = [NSKeyedUnarchiver unarchiveObjectWithData:udObject] ;

如果一个自定义类中由另一个自定义类对象,那么所有嵌套的类都要实现。

图片二:保存了数据,第二次打开所看到的启动初始界面

-(void)defaultCheck:(id)sender
{

NSLog(@“ check按钮被按下 ”);
NSUserDefaults accountDefaults = [NSUserDefaults standardUserDefaults];
if ([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == NO)
{
((UIButton
)sender).selected = YES;
[accountDefaults setBool:YES forKey:NCUserDefaultBoolForRegister];
[accountDefaults setObject:nameField.text forKey:NCUserDefaultNameKey];
[accountDefaults setObject:pwdField.text forKey:NCUserDefaultPasswordKey];
}
else {//YES
((UIButton*)sender).selected = NO;
[accountDefaults setBool:NO forKey:NCUserDefaultBoolForRegister];
[accountDefaults setObject:nil forKey:NCUserDefaultNameKey];
[accountDefaults setObject:nil forKey:NCUserDefaultPasswordKey];
}
}

-(void)loadView {
[super loadView];

nameField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
pwdField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
[nameField setCenter:CGPointMake(160, 100)];
[pwdField setCenter:CGPointMake(160, 150)];

NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];

//存的是bool值一定要用boolForKey:获取
//如果之前已经保存了User Defaults,那么在读取的时候就要将数据读取出来,显示在用户名和密码框中(这里没有加密)。

if ([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == YES) {
nameField.text = [accountDefaults objectForKey:NCUserDefaultNameKey];
pwdField.text = [accountDefaults objectForKey:NCUserDefaultPasswordKey];
}
else
{
[nameField setPlaceholder:@“用户名”];
[pwdField setPlaceholder:@“密码”];
}

nameField.borderStyle = UITextBorderStyleBezel;
[pwdField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:nameField];
[self.view addSubview:pwdField];

UIButton * loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginBtn setFrame:CGRectMake(0, 0, 50, 30)];
[loginBtn setCenter:CGPointMake(200, 200)];
[loginBtn addTarget:self action:@selector(loginToWeiBo:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];

UIButton * checkBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[checkBtn setFrame:CGRectMake(0, 0, 20, 20)];
[checkBtn setCenter:CGPointMake(160, 180)];

//选中之后得图标
[checkBtn setBackgroundImage:[UIImage imageNamed:@“lock.png”] forState:(UIControlState)UIControlStateHighlighted];
[checkBtn setBackgroundImage:[UIImage imageNamed:@“lock.png”] forState:(UIControlState)UIControlStateSelected];

//未选中
[checkBtn setBackgroundImage:[UIImage imageNamed:@“unlock.png”] forState:(UIControlState)UIControlStateNormal];
[checkBtn addTarget:self action:@selector(defaultCheck:) forControlEvents:UIControlEventTouchUpInside];

//用户名和密码已经保存
if([accountDefaults boolForKey:NCUserDefaultBoolForRegister] == YES) 
{
[checkBtn setSelected:YES];
}

[self.view addSubview:Btn];
}

IOS问题汇总:2015-1-9 NSUserDefault (转)

标签:

原文地址:http://www.cnblogs.com/hanyutong/p/4427612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!