如何存储自定义类到UserDefault中
2个回答
展开全部
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *UserName;
@property (weak, nonatomic) IBOutlet UILabel *PassWord;
@property (weak, nonatomic) IBOutlet UILabel *a1;
@property (weak, nonatomic) IBOutlet UILabel *a2;
@end
@implementation ViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.UserName = nil;
self.PassWord = nil;
}
//存储数据
-(void)saveNSUserDefaults {
//获得当前输入框的账号密码
NSString *string = self.UserName.text;
NSString *sth = self.PassWord.text;
//创建数据储存器,并按照KVC方式储存
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:string forKey:@"UserName"];
[userDefaults setObject:sth forKey:@"PossWord"];
}
-(void)readNSUserDefault {
//创建数据储存器
NSUserDefaults *Udefault = [NSUserDefaults standardUserDefaults];
//读取数据到各个label中
NSString *stt = [Udefault stringForKey:@"UserName"];
NSString *sst = [Udefault stringForKey:@"PossWord"];
self.UserName.text = stt;
self.PassWord.text = sst;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
storeBoard创建的,你可以复制粘贴改一下再用,需要注意的是无论是存储还是删除还是读取时,nsnumber类型永远都是本身类型,也就说是自己的基本数据类型,如:
读取整型int类型的数据 NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
而其他类型全向上文代码一样以对象类型存储,全手打望采纳
@property (weak, nonatomic) IBOutlet UILabel *UserName;
@property (weak, nonatomic) IBOutlet UILabel *PassWord;
@property (weak, nonatomic) IBOutlet UILabel *a1;
@property (weak, nonatomic) IBOutlet UILabel *a2;
@end
@implementation ViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.UserName = nil;
self.PassWord = nil;
}
//存储数据
-(void)saveNSUserDefaults {
//获得当前输入框的账号密码
NSString *string = self.UserName.text;
NSString *sth = self.PassWord.text;
//创建数据储存器,并按照KVC方式储存
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:string forKey:@"UserName"];
[userDefaults setObject:sth forKey:@"PossWord"];
}
-(void)readNSUserDefault {
//创建数据储存器
NSUserDefaults *Udefault = [NSUserDefaults standardUserDefaults];
//读取数据到各个label中
NSString *stt = [Udefault stringForKey:@"UserName"];
NSString *sst = [Udefault stringForKey:@"PossWord"];
self.UserName.text = stt;
self.PassWord.text = sst;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
storeBoard创建的,你可以复制粘贴改一下再用,需要注意的是无论是存储还是删除还是读取时,nsnumber类型永远都是本身类型,也就说是自己的基本数据类型,如:
读取整型int类型的数据 NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
而其他类型全向上文代码一样以对象类型存储,全手打望采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-08-30
展开全部
自定义类要实现要实现-
复制代码
(void)encodeWithCoder:(NSCoder *)coder
-
(id)initWithCoder:(NSCoder *)coder
然后使用
NSArchiver和NSUnArchiver保存
复制代码
(void)encodeWithCoder:(NSCoder *)coder
-
(id)initWithCoder:(NSCoder *)coder
然后使用
NSArchiver和NSUnArchiver保存
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询