57.使用Preferences保存用户名和密码

 

注意:如果勾选了Use Size Classes,上图中用于登录的Segue的push就得换成show。 

#import "LXViewController.h"

@interface LXViewController ()
@property (weak, nonatomic) IBOutlet UITextField *account;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *LoginBtn;
- (
IBAction)LoginClick;

@end

@implementation LXViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
    [
self.account addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [
self.password addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [
self loadUserData];
}

- (
void) textChange{
   
self.LoginBtn.enabled = self.account.text.length && self.password.text.length;
}

- (
IBAction)LoginClick {
   
NSString *account = self.account.text;
   
NSString *password = self.password.text;
   
if (![account isEqualToString:@"lx"]) {
       
return;
    }
   
if (![password isEqualToString:@"123"]) {
       
return;
    }
    [
self saveUserData];
    [
self.view endEditing:YES];
    [
self performSegueWithIdentifier:@"login" sender:nil];
}

- (
void)loadUserData{
   
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   
self.account.text = [defaults objectForKey:@"account"];
   
self.password.text = [defaults objectForKey:@"password"];
}

- (
void)saveUserData{
   
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults
setObject:self.account.text forKey:@"account"];
    [defaults
setObject:self.password.text forKey:@"password"];
    [defaults
synchronize];
}

@end
posted @ 2015-10-10 15:53  我要选李白  阅读(209)  评论(0编辑  收藏  举报