您的位置:首页 > 移动开发 > IOS开发

登陆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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息