码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-10 17:01:03      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

注意:如果勾选了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

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

标签:

原文地址:http://www.cnblogs.com/lixiang2015/p/4867297.html

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