UI基础

iOS实战项目开发:通讯录

2016-03-21  本文已影响241人  952625a28d0d

storyBoard实现页面跳转布局

Paste_Image.png Paste_Image.png

实现输入框的监听

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addobser];
}

- (void)addobser{
    // 注册通知 添加观察者
    /*
     observer:给self添加 的意思是针对本控制器
     selector:调用的方法
     name:表示对哪一个属性观察
     object:表示要观察的对象
     */
    //    添加用户名输入框观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    //    添加密码输入框观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.passwordField];
}```

pragma mark - 文本框变化后调用的方法

效果:

login.gif

实现Segue的传值 和 跳转

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #4f8187}

#pragma mark - Navigation 跳转之前的准备工作
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    //1:取得目标控制器
    UIViewController *contactVC = segue.destinationViewController;
    // 2:设置标题(传值)
    contactVC.title = [NSString stringWithFormat:@"%@的联系人列表",self.nameField.text];
}

#pragma mark - 当用户点击之后 实现传值和跳转工作
- (IBAction)loginOnclick:(id)sender {
    // 执行跳转
    [self performSegueWithIdentifier:@"LoginToContact" sender:nil];
}```

效果

![segus.gif](http:https://img.haomeiwen.com/i189984/1597cea29f203b52.gif?imageMogr2/auto-orient/strip)

###UIAlertViewController与添加页面布局

}```

exit.gif

添加功能与传值

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}

#import <UIKit/UIKit.h>

@class AddTableViewController,ContactModel;

@protocol AddVCDelegate <NSObject>

@optional

- (void)addContact:(AddTableViewController *)addVC didAddContact:(ContactModel *)model;

@end

@interface AddTableViewController : UIViewController

@property (nonatomic, weak) id<AddVCDelegate> delegate;

@end```

import "AddTableViewController.h"

import "ContactModel.h"

@interface AddTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *addContactButton; // 添加联系人按钮

@end

@implementation AddTableViewController

pragma mark - 视图已经出现 让输入框成为第一响应者

@end```

联系人控制器 获取并刷新数据

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s6 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s10 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    // 拿到目的控制器
    id vc = segue.destinationViewController;
    // 判断是哪一个控制器
    if ([vc isKindOfClass:[AddTableViewController class]]) {
        // 如果是跳转到添加联系人控制器 设置代理
        AddTableViewController *addVc = vc;
        addVc.delegate = self;
    }else if ([vc isKindOfClass:[EditTableViewController class]]){
        EditTableViewController *editVC = vc;
        // 取得选中的那一行
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        // 数据传入
        editVC.contactModel = self.contactArr[path.row];
        // 设置代理
        editVC.delegate = self;
    }
    
    
    // 拿到控制器
//    AddTableViewController *addVC = segue.destinationViewController;
    // 设置代理
//    addVC.delegate = self;
}```

pragma mark - addContactDelegate

add.gif

数据归档存储

// 数据存储
#define User_Name_Key @"name"
#define PWD_key @"password"
#define RemPwdKry @"rem_pwd"```

- 读取数据

#pragma mark - 当用户点击之后 实现传值和跳转工作
- (IBAction)loginOnclick:(id)sender {
    if (![self.nameField.text isEqualToString:@"jafar"]) {
        [MBProgressHUD showError:@"账号不存在"];
        return;
    }
    if (![self.passwordField.text isEqualToString:@"jyf2015."]){
        [MBProgressHUD showError:@"密码错误"];
        return;
    }
    // 显示蒙版
    [MBProgressHUD showMessage:@"正在登录"];
    // 模拟两秒时间,结束跳转
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 移除遮盖
        [MBProgressHUD hideHUD];
        // 执行跳转
        [self performSegueWithIdentifier:@"LoginToContact" sender:nil];
    });
    
    // 存储用户名和密码
    NSUserDefaults *userDefult = [NSUserDefaults standardUserDefaults];
    [userDefult setValue:self.nameField.text forKey:User_Name_Key];
    [userDefult setValue:self.passwordField.text forKey:PWD_key];
    [userDefult setBool:self.remberSwitch.isOn forKey:RemPwdKry];
    [userDefult synchronize];   // 同步
}```

![save.gif](http:https://img.haomeiwen.com/i189984/4943bea5e80ec6d4.gif?imageMogr2/auto-orient/strip)


####数据模型的存储方式

- 归档  遵守归档协议 编写路径宏定义 存储  取出  展示  可以存储数组类型哦

import <Foundation/Foundation.h>

@interface ContactModel : NSObject<NSCoding>

@property(nonatomic, copy) NSString * name;
@property(nonatomic, copy) NSString * phone;

@end```

#import "ContactModel.h"

@implementation ContactModel

/*
 将某个对象写入文件是会调用
 在这个方法中说清楚哪些属性需要存储
 */
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
}

/*
 解析对象的时候会调用这个方法
 需要解析哪些属性
 */

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
    }
    return self;
}
@end```


//路径

define ContactFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"contacts.data"]```

@interface ContactTableViewController ()<AddVCDelegate,EditTVCDelegate>

@property (nonatomic, strong) NSMutableArray *contactArr;   // 联系人数组

@end

@implementation ContactTableViewController

#pragma mark - 懒加载
- (NSMutableArray *)contactArr{
    if (!_contactArr) {
        // 数据解码
        _contactArr = [NSKeyedUnarchiver unarchiveObjectWithFile:ContactFilePath];
        if (_contactArr == nil) {
            _contactArr = [NSMutableArray array];
        }
    }
    return _contactArr;
    
}```

- 存储 这里注意 不管是添加联系人还是编辑修改之后的数据都要重新归档

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s9 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s10 {font-variant-ligatures: no-common-ligatures; color: #78492a}

pragma mark - addContactDelegate

pragma mark - editContactDelegate

archive.gif

数据编辑方面的逻辑

#import <UIKit/UIKit.h>

@class ContactModel,EditTableViewController;

@protocol EditTVCDelegate <NSObject>

@optional

- (void)editVC:(EditTableViewController *)editVC contactModel:(ContactModel *)model;

@end

@interface EditTableViewController : UIViewController

@property (nonatomic, strong) ContactModel *contactModel;

@property (nonatomic, weak) id<EditTVCDelegate> delegate;

@end```


pragma mark - 编辑按钮

pragma mark - 保存按钮

pragma mark - 文本验证后调用的方法

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

@end```

主控制器的刷新

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s7 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s8 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s11 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s12 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}

#pragma mark - editContactDelegate
- (void)editVC:(EditTableViewController *)editVC contactModel:(ContactModel *)model{
    [self.tableView reloadData];
    
    [NSKeyedArchiver archiveRootObject:self.contactArr toFile:ContactFilePath];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    // 拿到目的控制器
    id vc = segue.destinationViewController;
    // 判断是哪一个控制器
    if ([vc isKindOfClass:[AddTableViewController class]]) {
        // 如果是跳转到添加联系人控制器 设置代理
        AddTableViewController *addVc = vc;
        addVc.delegate = self;
    }else if ([vc isKindOfClass:[EditTableViewController class]]){
        EditTableViewController *editVC = vc;
        // 取得选中的那一行
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        // 数据传入
        editVC.contactModel = self.contactArr[path.row];
        // 设置代理
        editVC.delegate = self;
    }
    
    
    // 拿到控制器
//    AddTableViewController *addVC = segue.destinationViewController;
    // 设置代理
//    addVC.delegate = self;
}```


####删除逻辑  注意数据的更新 和 视图的更新

pragma mark - 调用代理 实现删除功能

delegate.gif
上一篇下一篇

猜你喜欢

热点阅读