登陆Demo--使用UserDeaults实现自动登录,传值,了解数据持久化方法
2015-12-03 11:01
706 查看
要实现登陆demo的自动保存登录信息,登录后显示用户名。
页面间传值的方法有:属性传值,Block传值,代理传值,单例传值,通知传值等,属性列表里,用UserDefaults进行存值和取值,是能比较简单快速的保存轻量级本地数据的方法,比如登录界面的数据,用户名,密码之类的,下次再登录的时候就可以直接从NSUserDefaults里面读取上次登录的信息。它不需要用户在程序中设置UserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,就在哪里创建一个NSUserDefaults的对象,进行读写操作。
1/UISwitch开关选择是否保存用户名和密码
2/点击登录按钮,保存配置信息
3/申明一个Bool变量“isLogin"判断是否是第一次登录,是第一次登录显示框显示提示信息,非第一次登录显示读取的配置信息
1.ViewController.m的关键代码:
#import "ViewController.h"
#import "RightViewController.h"
#import "NewViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)loadView{
[super loadView];
NSLog(@"Hello");
}
- (void)viewDidLoad {
[super
viewDidLoad];
[self
initViews];
// [self readNSUserDefaults]; //调用此方法从NSUserDefautls中读取各种数据,在下面定义
//存的是bool值一定要用boolForKey:获取
//如果之前已经保存了UserDefaults,那么在读取的时候就要将数据读取出来,显示在用户名和密码框中
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
if ([userDefaults
boolForKey:@"isLogin"] ==
YES) {
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
usernameTextField.text = [userDefaults
objectForKey:@"name"];
passwordTextField.text = [userDefaults
objectForKey:@"password"];
}
else
{
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
[usernameTextField setPlaceholder:@"请输入用户名"];
[passwordTextField setPlaceholder:@"请输入密码"];
}
}
-(void)initViews
{
UILabel *label=[[UILabel
alloc] initWithFrame:CGRectMake(100,
100, 320,
40)];
label.text=@"用户名:";
[self.view
addSubview:label];
UILabel *label2=[[UILabel
alloc] initWithFrame:CGRectMake(100,
150, 320,
40)];
label2.text=@"密码:";
[self.view
addSubview:label2];
UILabel *label3=[[UILabel
alloc] initWithFrame:CGRectMake(180,
195, 100,
40)];
label3.text=@"记住密码";
label3.textColor=[UIColor
blueColor];
[self.view
addSubview:label3];
UITextField *usernameTextField=[[UITextField
alloc] initWithFrame:CGRectMake(165,
100, 150,
40)];
usernameTextField.keyboardType=UIKeyboardTypeNamePhonePad;
usernameTextField.borderStyle=UITextBorderStyleRoundedRect;
usernameTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
// textfield.placeholder=@"请输入用户名";
usernameTextField.secureTextEntry=NO;
usernameTextField.textColor=[UIColor
blackColor];
usernameTextField.tag=100;//定义tag值,在后面用tag传递usernameTextField.text
//也可以把usernameTextField写成全局变量
[self.view
addSubview:usernameTextField];
UITextField *passwordTextField=[[UITextField
alloc] initWithFrame:CGRectMake(165,
150, 150,
40)];
passwordTextField.keyboardType=UIKeyboardTypeNamePhonePad;
passwordTextField.borderStyle=UITextBorderStyleRoundedRect;
passwordTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
// textfield2.placeholder=@"请输入密码";
passwordTextField.secureTextEntry=YES;
passwordTextField.textColor=[UIColor
blackColor];
passwordTextField.tag=101;//定义tag值,在后面用tag传递passwordTextField.text
//也可以把passwordTextField写成全局变量
[self.view
addSubview:passwordTextField];
UIButton *loginButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame=CGRectMake(100,
240, 200,
40);
[loginButton setTitle:@"登录"f
10533
orState:UIControlStateNormal];
[loginButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[loginButton addTarget:self
action:@selector(jumpSuccess)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:loginButton];
UIButton *registerButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
registerButton.frame=CGRectMake(100,
280, 200,
40);
[registerButton setTitle:@"注册"forState:UIControlStateNormal];
[registerButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[registerButton addTarget:self
action:@selector(jumpNew)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:registerButton];
UISwitch *rembSwitch = [[UISwitch
alloc] initWithFrame:CGRectMake(260,
200, 100,
40)];
rembSwitch.on=YES;
[rembSwitch addTarget:self
action:@selector(onSwitch:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:rembSwitch];
}
- (void)onSwitch:(id)sender {
UISwitch *rembSwitch = sender;
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
//点击开关,变化开关状态选择是否保存密码
[userDefaults setBool:rembSwitch.on
forKey: @"saveLogin"];
[userDefaults synchronize];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)jumpSuccess{
RightViewController *rightvc=[[RightViewController
alloc]init];
rightvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self
presentViewController:rightvc animated:YES
completion:^{
}];
// [self saveNSUserDefaults]; //调用此方法将各种数据存储到NSUserDefautls中,在下面定义
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
if ([userDefaults
boolForKey:@"saveLogin"] ==
YES)
{
NSLog(@"1");
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
[userDefaults setObject:usernameTextField.text
forKey:@"name"];
[userDefaults setObject:passwordTextField.text
forKey:@"password"];
[userDefaults setBool:YES
forKey:@"isLogin"];
[userDefaults synchronize];
}
else {
[userDefaults setBool:NO
forKey:@"saveLogin"];
NSLog(@"1-1");
}
}
-(void)jumpNew{
NewViewController *newvc=[[NewViewController
alloc]init];
newvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self
presentViewController:newvc animated:YES
completion:^{
}];
}
/*-(void)viewDidAppear:(BOOL)animated{
}*/
@end
2.RightViewController.m的关键代码:
#import "ViewController.h"
#import "RightViewController.h"
@interface
RightViewController ()
@end
@implementation RightViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view from its nib.
UILabel *nameString=[[UILabel
alloc] initWithFrame:CGRectMake(170,
150, 320,
40)];
nameString.backgroundColor=[UIColor
clearColor];
nameString.textColor=[UIColor
redColor];
nameString.text=@"hello,";
//从UserDefaults里读取"name"的数据到"namestring"Label显示
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
nameString.text=[userDefaults
objectForKey:@"name"];
[self.view
addSubview:nameString];
UIButton *okButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
okButton.frame=CGRectMake(150,
250, 100,
40);
[okButton setTitle:@"OK"
forState:UIControlStateNormal];
[okButton addTarget:self
action:@selector(close)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:okButton];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)close{
[self
dismissViewControllerAnimated:YES
completion:^{
}];
}
@end
页面间传值的方法有:属性传值,Block传值,代理传值,单例传值,通知传值等,属性列表里,用UserDefaults进行存值和取值,是能比较简单快速的保存轻量级本地数据的方法,比如登录界面的数据,用户名,密码之类的,下次再登录的时候就可以直接从NSUserDefaults里面读取上次登录的信息。它不需要用户在程序中设置UserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,就在哪里创建一个NSUserDefaults的对象,进行读写操作。
1/UISwitch开关选择是否保存用户名和密码
2/点击登录按钮,保存配置信息
3/申明一个Bool变量“isLogin"判断是否是第一次登录,是第一次登录显示框显示提示信息,非第一次登录显示读取的配置信息
1.ViewController.m的关键代码:
#import "ViewController.h"
#import "RightViewController.h"
#import "NewViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
-(void)loadView{
[super loadView];
NSLog(@"Hello");
}
- (void)viewDidLoad {
[super
viewDidLoad];
[self
initViews];
// [self readNSUserDefaults]; //调用此方法从NSUserDefautls中读取各种数据,在下面定义
//存的是bool值一定要用boolForKey:获取
//如果之前已经保存了UserDefaults,那么在读取的时候就要将数据读取出来,显示在用户名和密码框中
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
if ([userDefaults
boolForKey:@"isLogin"] ==
YES) {
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
usernameTextField.text = [userDefaults
objectForKey:@"name"];
passwordTextField.text = [userDefaults
objectForKey:@"password"];
}
else
{
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
[usernameTextField setPlaceholder:@"请输入用户名"];
[passwordTextField setPlaceholder:@"请输入密码"];
}
}
-(void)initViews
{
UILabel *label=[[UILabel
alloc] initWithFrame:CGRectMake(100,
100, 320,
40)];
label.text=@"用户名:";
[self.view
addSubview:label];
UILabel *label2=[[UILabel
alloc] initWithFrame:CGRectMake(100,
150, 320,
40)];
label2.text=@"密码:";
[self.view
addSubview:label2];
UILabel *label3=[[UILabel
alloc] initWithFrame:CGRectMake(180,
195, 100,
40)];
label3.text=@"记住密码";
label3.textColor=[UIColor
blueColor];
[self.view
addSubview:label3];
UITextField *usernameTextField=[[UITextField
alloc] initWithFrame:CGRectMake(165,
100, 150,
40)];
usernameTextField.keyboardType=UIKeyboardTypeNamePhonePad;
usernameTextField.borderStyle=UITextBorderStyleRoundedRect;
usernameTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
// textfield.placeholder=@"请输入用户名";
usernameTextField.secureTextEntry=NO;
usernameTextField.textColor=[UIColor
blackColor];
usernameTextField.tag=100;//定义tag值,在后面用tag传递usernameTextField.text
//也可以把usernameTextField写成全局变量
[self.view
addSubview:usernameTextField];
UITextField *passwordTextField=[[UITextField
alloc] initWithFrame:CGRectMake(165,
150, 150,
40)];
passwordTextField.keyboardType=UIKeyboardTypeNamePhonePad;
passwordTextField.borderStyle=UITextBorderStyleRoundedRect;
passwordTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
// textfield2.placeholder=@"请输入密码";
passwordTextField.secureTextEntry=YES;
passwordTextField.textColor=[UIColor
blackColor];
passwordTextField.tag=101;//定义tag值,在后面用tag传递passwordTextField.text
//也可以把passwordTextField写成全局变量
[self.view
addSubview:passwordTextField];
UIButton *loginButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame=CGRectMake(100,
240, 200,
40);
[loginButton setTitle:@"登录"f
10533
orState:UIControlStateNormal];
[loginButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[loginButton addTarget:self
action:@selector(jumpSuccess)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:loginButton];
UIButton *registerButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
registerButton.frame=CGRectMake(100,
280, 200,
40);
[registerButton setTitle:@"注册"forState:UIControlStateNormal];
[registerButton setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];
[registerButton addTarget:self
action:@selector(jumpNew)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:registerButton];
UISwitch *rembSwitch = [[UISwitch
alloc] initWithFrame:CGRectMake(260,
200, 100,
40)];
rembSwitch.on=YES;
[rembSwitch addTarget:self
action:@selector(onSwitch:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:rembSwitch];
}
- (void)onSwitch:(id)sender {
UISwitch *rembSwitch = sender;
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
//点击开关,变化开关状态选择是否保存密码
[userDefaults setBool:rembSwitch.on
forKey: @"saveLogin"];
[userDefaults synchronize];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)jumpSuccess{
RightViewController *rightvc=[[RightViewController
alloc]init];
rightvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self
presentViewController:rightvc animated:YES
completion:^{
}];
// [self saveNSUserDefaults]; //调用此方法将各种数据存储到NSUserDefautls中,在下面定义
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
if ([userDefaults
boolForKey:@"saveLogin"] ==
YES)
{
NSLog(@"1");
UITextField *usernameTextField=(UITextField *)[self.view
viewWithTag:100];
UITextField *passwordTextField=(UITextField *)[self.view
viewWithTag:101];
[userDefaults setObject:usernameTextField.text
forKey:@"name"];
[userDefaults setObject:passwordTextField.text
forKey:@"password"];
[userDefaults setBool:YES
forKey:@"isLogin"];
[userDefaults synchronize];
}
else {
[userDefaults setBool:NO
forKey:@"saveLogin"];
NSLog(@"1-1");
}
}
-(void)jumpNew{
NewViewController *newvc=[[NewViewController
alloc]init];
newvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[self
presentViewController:newvc animated:YES
completion:^{
}];
}
/*-(void)viewDidAppear:(BOOL)animated{
}*/
@end
2.RightViewController.m的关键代码:
#import "ViewController.h"
#import "RightViewController.h"
@interface
RightViewController ()
@end
@implementation RightViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view from its nib.
UILabel *nameString=[[UILabel
alloc] initWithFrame:CGRectMake(170,
150, 320,
40)];
nameString.backgroundColor=[UIColor
clearColor];
nameString.textColor=[UIColor
redColor];
nameString.text=@"hello,";
//从UserDefaults里读取"name"的数据到"namestring"Label显示
NSUserDefaults *userDefaults = [NSUserDefaults
standardUserDefaults];
nameString.text=[userDefaults
objectForKey:@"name"];
[self.view
addSubview:nameString];
UIButton *okButton=[UIButton
buttonWithType:UIButtonTypeRoundedRect];
okButton.frame=CGRectMake(150,
250, 100,
40);
[okButton setTitle:@"OK"
forState:UIControlStateNormal];
[okButton addTarget:self
action:@selector(close)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:okButton];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)close{
[self
dismissViewControllerAnimated:YES
completion:^{
}];
}
@end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器