03小码哥通讯录(数据存储)

 

//
//  XMGContact.m
//  小码哥通讯录
#import "XMGContact.h"

@implementation XMGContact


static NSString *nameKey = @"name";
static NSString *phoneKey = @"phone";

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    // name.phone
    [aCoder encodeObject:_name forKey:nameKey];
    
    [aCoder encodeObject:_phone forKey:phoneKey];
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        _name = [aDecoder decodeObjectForKey:nameKey];
        _phone = [aDecoder decodeObjectForKey:phoneKey];
        
    }
    
    return self;
}

+ (instancetype)contactWithName:(NSString *)name phone:(NSString *)phone
{
    XMGContact *c = [[self alloc] init];
    
    c.name = name;
    c.phone = phone;
    
    return c;
}
@end

 

//  XMGContactViewController.m
//  小码哥通讯录
#import "XMGContactViewController.h"
#import "XMGAddViewController.h"
#import "XMGEditViewController.h"
#import "XMGContact.h"

#define XMGFilePath  [ NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"contacts.data"]
@interface XMGContactViewController ()<UIActionSheetDelegate>

@property (nonatomic, strong) NSMutableArray *contacts;

@end

@implementation XMGContactViewController

- (NSMutableArray *)contacts
{
    if (_contacts == nil) {
        
        // 读取数据 接档接档接档
        _contacts = [NSKeyedUnarchiver unarchiveObjectWithFile:XMGFilePath];
        
        // 判断下有没有读取数据
        if (_contacts == nil) {
            _contacts = [NSMutableArray array];
        }
        
        
    }
    return _contacts;
}

// 跳转之前的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    
    // 给添加控制器传递联系人控制器属性
    XMGAddViewController *addVc = segue.destinationViewController;
    
    addVc.block = ^(XMGContact *contact){
        
        // 1.联系人添加到数组
        [self.contacts addObject:contact];
        
        
        // 2.刷新表格
        [self.tableView reloadData];
        
        // 3.保存联系人,注意:如果归档数组,底层会遍历数组元素一个一个归档
        [NSKeyedArchiver archiveRootObject:self.contacts toFile:XMGFilePath];
        
    };
    
    
}

// 点击注销的时候调用
- (IBAction)logout:(id)sender {
    
    // 弹出actionSheet
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"是否注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:nil, nil];
    
    [sheet showInView:self.view];
    
}
#pragma mark - UIActionSheetDelegate
// 点击UIActionSheet控件上的按钮调用
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) { // 点击了注销
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    // 取消分割线
   self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    // tableView有数据的时候才需要分割线
    
    // 开发小技巧:快速取消分割线
    self.tableView.tableFooterView = [[UIView alloc] init];
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.contacts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 创建标示符
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    // 获取模型
    XMGContact *c = self.contacts[indexPath.row];
    
    cell.textLabel.text = c.name;
    cell.detailTextLabel.text = c.phone;
    
    
    return cell;
}


#pragma mark - tableView代理方法
// 点击cell的时候调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 加载storyboard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    
    // 创建编辑控制器
     XMGEditViewController *editVc = [storyboard instantiateViewControllerWithIdentifier:@"edit"];
    
    
    editVc.contact = self.contacts[indexPath.row];
    
    // block:点击编辑的时候调用这个block
    editVc.block = ^(){ // 刷新表格
        [self.tableView reloadData];
        
        // 3.保存联系人,注意:如果归档数组,底层会遍历数组元素一个一个归档
        [NSKeyedArchiver archiveRootObject:self.contacts toFile:XMGFilePath];
    };
    
    // 跳转到编辑界面
    [self.navigationController pushViewController:editVc animated:YES];
    
}




// 只要实现这个方法,就会有滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 删除模型
    [self.contacts removeObjectAtIndex:indexPath.row];
    
    
    // 删除tableViewCell
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"增加" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 点击增加的时候调用
        NSLog(@"增加");
        
    }];
    action.backgroundColor = [UIColor greenColor];
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 点击增加的时候调用
        NSLog(@"删除");
        
    }];

    
    return @[action,action1];
}
@end

 

//
//  XMGLoginViewController.m
//  小码哥通讯录
//
//  Created by xiaomage on 15/6/12.
//  Copyright (c) 2015年 xiaomage. All rights reserved.
//

#import "XMGLoginViewController.h"

#import "MBProgressHUD+XMG.h"

@interface XMGLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UITextField *accountField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@property (weak, nonatomic) IBOutlet UISwitch *rmbPwdSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch;
@end
/*
 来源控制器传递给目的控制器:顺传
 数据传值:
 1.接收方一定要有属性接收
 2.传递方必须要拿到接收方
 
 */
/*
 1.[self performSegueWithIdentifier]
 2.创建segue
 3.设置来源控制器segue.sourceViewController = self
 4.创建目的控制器,segue.destinationViewController = 目的控制器
 5.[self prepareForSegue]跳转之前的准备操作
 6.[segue perform]
 7.判断下segue的类型,如果是push,拿到导航控制器push
 [self.navigationController pushViewController:segue.destinationViewController animated:YES];
 */
@implementation XMGLoginViewController


#define XMGUserDefaults [NSUserDefaults standardUserDefaults]

static NSString *accountKey = @"account";
static NSString *pwdKey = @"pwd";
static NSString *rmbKey = @"rmd";
static NSString *loginKey = @"login";

// 在执行跳转之前的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    

    
    UIViewController *vc = segue.destinationViewController;
    vc.title = [NSString stringWithFormat:@"%@的联系人列表", _accountField.text];
    NSLog(@"%@--%@",segue.sourceViewController,segue.destinationViewController);
}

// 点击了登录按钮的时候调用
// xmg 123
- (IBAction)login:(id)sender {
    
    // 提示用户,正在登录ing...
    [MBProgressHUD showMessage:@"正在登录ing..."];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 隐藏蒙版
        [MBProgressHUD hideHUD];
        
        // 验证下账号和密码是否正确
        if ([_accountField.text isEqualToString:@"xmg"] && [_pwdField.text isEqualToString:@"123"]) { // 输入正确
            
            // 数据存储
            
            // 账号,密码,记住密码,自动登录
            [XMGUserDefaults setObject:_accountField.text forKey:accountKey];
            [XMGUserDefaults setObject:_pwdField.text forKey:pwdKey];
            [XMGUserDefaults setBool:_rmbPwdSwitch.on forKey:rmbKey];
            [XMGUserDefaults setBool:_autoLoginSwitch.on forKey:loginKey];
            
            
            // 直接跳转
            // 跳转到联系人界面
            [self performSegueWithIdentifier:@"login2Contact" sender:nil];
            
        }else{ // 账号或者密码错误
            
            // 提示用户账号或者密码错误
            [MBProgressHUD showError:@"账号或者密码错误"];
            
        }
        
    });
    
    
    
}

// 记住密码开关状态改变的时候调用
- (IBAction)rmbPwdChange:(id)sender {
    // 如果取消记住密码,自动登录也需要取消勾选
    
    if (_rmbPwdSwitch.on == NO) { // 取消记住密码
        // 取消自动登录
        [_autoLoginSwitch setOn:NO animated:YES];
    }
    
    
}

// 自动登录开关状态改变的时候调用
- (IBAction)autoLoginChange:(id)sender {
    
    // 如果勾选了自动登录,记住密码也要勾选
    if (_autoLoginSwitch.on == YES) {
        [_rmbPwdSwitch setOn:YES animated:YES];
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 读取数据
    NSString *account = [XMGUserDefaults objectForKey:accountKey];
    NSString *pwd = [XMGUserDefaults objectForKey:pwdKey];
    BOOL rmb = [XMGUserDefaults boolForKey:rmbKey];
    BOOL login = [XMGUserDefaults boolForKey:loginKey];
    
    _accountField.text = account;
    
    if (rmb == YES) {
        _pwdField.text = pwd;
    }
    
    _rmbPwdSwitch.on = rmb;
    _autoLoginSwitch.on = login;
    
    // 勾选自动登录
    if (login == YES) {
        
        [self login:nil];
        
    }
    
    // 给文本框添加监听器,及时监听文本框内容的改变
    [_accountField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    [_pwdField addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
    
    // 判断下登录按钮能否点击
    [self textChange];
    
}



// 任一一个文本框的内容改变都会调用
- (void)textChange
{
    _loginBtn.enabled = _accountField.text.length && _pwdField.text.length;
    
    NSLog(@"%@--%@",_accountField.text,_pwdField.text);
}


@end

 

转载于:https://www.cnblogs.com/laugh/p/6651386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值