码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发 - 私人通讯录实例

时间:2015-08-08 09:10:17      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:通讯录   ios开发   uiviewcontroller   mvc   

私人通讯录实例

此实例主要练习UIViewController控制器的使用

创建模型类Model

/**
 copy : NSString\NSMutableString\block
 weak : 代理\UI控件
 strong : 其他OC对象
 assign : 基本数据类型(int\float)\枚举\结构体
 */
#import <Foundation/Foundation.h>

@interface ZLContact : NSObject
@property(nonatomic,copy) NSString * name;
@property(nonatomic,copy) NSString * phone;

@end

创建视图类View

#import <UIKit/UIKit.h>

@class ZLContact;
@interface ZLContactCell : UITableViewCell
@property(nonatomic,strong) ZLContact * contact;

+ (instancetype)cellWithTableView: (UITableView *)tableView;

@end
#import "ZLContactCell.h"
#import "ZLContact.h"

@interface ZLContactCell ()
@property(nonatomic,weak) UIView *divider;

@end

@implementation ZLContactCell

+ (instancetype)cellWithTableView: (UITableView *)tableView
{
    static NSString *ID =@"contact";
    // 先从缓存池中取,如果缓存池中没有可循环利用的cell,先去storyboard中找到合适的cell
    // cell是从storyboard中创建出来的
    return [tableView dequeueReusableCellWithIdentifier:ID];
}

/**
 *  如果cell是通过storyboard或者xib创建的,就不可能会调用这个方法来初始化cell
 *  如果cell是通过手写代码创建,才会调用这个方法来初始化cell
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    }
    return self;
}

/**
 *  如果cell是通过storyboard或者xib创建的,就会调用这个方法来初始化cell
 *  这个方法的作用类似于init方法
 */
- (void)awakeFromNib
{
    // Initialization code
    UIView *divider = [[UIView alloc] init];
    divider.backgroundColor = [UIColor blackColor];
    divider.alpha = 0.2;
    [self.contentView addSubview:divider];
    self.divider = divider;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat dividerX=0;
    CGFloat dividerH=1;
    CGFloat dividerY=self.frame.size.height - dividerH;
    CGFloat dividerW=self.frame.size.width;
    self.divider.frame=CGRectMake(dividerX, dividerY, dividerW, dividerH);

}

- (void)setContact:(ZLContact *)contact
{
    _contact=contact;

    self.textLabel.text=contact.name;
    self.detailTextLabel.text=contact.phone;
}
@end

创建控制器类ViewController

登陆控制器 LoginViewController

#import "ZLLoginViewController.h"
#import "MBProgressHUD+MJ.h"
@interface ZLLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UISwitch *rmbPwdSwitch;
- (IBAction)rmbPwdChange;

@property (weak, nonatomic) IBOutlet UISwitch *autoLoginSwitch;
- (IBAction)autoLoginChange;

- (IBAction)login;


@end

@implementation ZLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdField];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/**
 *  文本框的文字发生改变的时候调用
 */
- (void)textChange
{
//    self.loginBtn.enabled=(self.accountField.text.length&&self.pwdField.text.length);
        self.loginBtn.enabled=YES;

}

/**
 *  记住密码开关的状态改变就会调用
 */
- (IBAction)rmbPwdChange {

    //取消了记住密码
    if (self.rmbPwdSwitch.isOn==NO) {
        self.autoLoginSwitch.on=NO;
    }
}

/**
 *  自动登录的状态改变就会调用
 */
- (IBAction)autoLoginChange {
    if (self.autoLoginSwitch.isOn) {
        self.rmbPwdSwitch.on=YES;
    }
}

/**
 *  登录
 */
- (IBAction)login {
    if (![self.accountField.text isEqualToString:@"lihui"]) {
        //账号不存在
        [MBProgressHUD showError:@"账号不存在"];
        return;
    }
    if (![self.pwdField.text isEqualToString:@"123"]) {
        //密码错误
        [MBProgressHUD showError:@"密码错误"];
        return;
    }

    //显示一个蒙版(遮盖)
    [MBProgressHUD showMessage:@"正在玩命加载..."];

    //模拟2秒后执行跳转
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //移除遮盖
        [MBProgressHUD hideHUD];

        //跳转 --执行login2contacts这个segue
        [self performSegueWithIdentifier:@"login2contacts" sender:nil];
    });
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //1.取得目标控制器(联系人列表的控制器)
    UIViewController *contactVc=segue.destinationViewController;

    //2.设置标题
    contactVc.title=[NSString stringWithFormat:@"%@的联系人列表",self.accountField.text];

}

@end

联系人控制器ContactsViewController

#import "ZLContactsViewController.h"
#import "ZLAddViewController.h"
#import "ZLEditViewController.h"
#import "ZLContact.h"
#import "ZLContactCell.h"

@interface ZLContactsViewController ()<UIActionSheetDelegate,ZLAddViewControllerDelegate,ZLEditViewControllerDelegate>

- (IBAction)logout:(id)sender;

@property (nonatomic,strong)NSMutableArray * contacts;
@end

@implementation ZLContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
}


- (NSMutableArray *)contacts
{
    if (_contacts==nil) {
        _contacts = [NSMutableArray array];
    }
    return _contacts;
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contacts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.创建cell
    ZLContactCell *  cell=[ZLContactCell cellWithTableView:tableView];

    //2.设置cell的数据
    cell.contact = self.contacts[indexPath.row];

    return cell;
}

/**
 *  注销
 */
- (IBAction)logout:(id)sender {

    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"确定要注销?" delegate:self
    cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
}


#pragma mark - actionsheet的代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex!=0)return;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id vc=segue.destinationViewController;

    if ([vc isKindOfClass:[ZLAddViewController class]]) {
        //设置下一个控制器(添加联系人的控制器)的代理
        ZLAddViewController * addVc=vc;
        addVc.delegate=self;
    }else if ([vc isKindOfClass:[ZLEditViewController class]]){
        ZLEditViewController * editVc=vc;
        //取得选中的那行
        NSIndexPath *path=[self.tableView indexPathForSelectedRow];
        editVc.contact=self.contacts[path.row];
        editVc.delegate=self;

    }
}

#pragma mark - ZLAddViewController的代理方法
- (void)addViewController:(ZLAddViewController *)addVc didAddContact:(ZLContact *)contact
{
    //1.添加数据模型
    [self.contacts addObject:contact];

    //2.刷新数据
    [self.tableView reloadData];
}
#pragma mark - ZLEditViewController的代理方法
- (void)editViewController:(ZLEditViewController *)editVc didSaveContact:(ZLContact *)contact
{
    [self.tableView reloadData];
}
@end

添加联系人控制器AddViewController

#import <UIKit/UIKit.h>
@class ZLAddViewController,ZLContact;
/**
 *  定义添加控制器的协议
 */
@protocol ZLAddViewControllerDelegate <NSObject>

@optional
/**
 *  在协议定义要实现的方法(optional)
 */
- (void)addViewController:(ZLAddViewController *)addVc didAddContact:(ZLContact *)contact;

@end
@interface ZLAddViewController : UIViewController

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

@end
#import "ZLAddViewController.h"
#import "ZLContact.h"
@interface ZLAddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

- (IBAction)add;

@end

@implementation ZLAddViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneField];

    // 退出键盘
    //    [self.nameField resignFirstResponder];
    //    [self.view endEditing:YES];
}

/**
 *  控制器的view完全显示的时候调用
 */
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    //让姓名文本框成为第一响应者(叫出键盘)
    [self.nameField becomeFirstResponder];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 *  文本框的文字发生改变的时候调用
 */

- (void)textChange
{
    self.addBtn.enabled=(self.nameField.text.length &&self.phoneField.text.length);
}

/**
 *  添加
 */
- (IBAction)add {

    //1.关闭当前控制器
    [self.navigationController popViewControllerAnimated:YES];

    //2.传递数据给上一个控制器(ZLContactsViewController)
    //2.通知代理
    if ([self.delegate respondsToSelector:@selector(addViewController:didAddContact:)]) {
        ZLContact *contact =[[ZLContact alloc]init];
        contact.name=self.nameField.text;
        contact.phone=self.phoneField.text;
        [self.delegate addViewController:self didAddContact:contact];

    }
}
@end

编辑联系人控制器EditViewController

#import <UIKit/UIKit.h>

@class ZLContact,ZLEditViewController;

@protocol ZLEditViewControllerDelegate <NSObject>

@optional

- (void)editViewController:(ZLEditViewController *)editVc didSaveContact:(ZLContact *)contact;

@end
@interface ZLEditViewController : UIViewController

@property(nonatomic,strong) ZLContact * contact;
@property(nonatomic,weak) id <ZLEditViewControllerDelegate> delegate;

@end
#import "ZLEditViewController.h"
#import "ZLContact.h"
@interface ZLEditViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
- (IBAction)edit:(UIBarButtonItem *)item;


- (IBAction)save;

@end

@implementation ZLEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置数据
    self.nameField.text=self.contact.name;
    self.phoneField.text=self.contact.phone;

    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneField];

}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 *  文本框的文字发生改变时候调用
 */
- (void)textChange
{
    self.saveBtn.enabled=(self.nameField.text.length&&self.phoneField.text.length);

}

- (IBAction)edit:(UIBarButtonItem *)item {
    if (self.nameField.enabled) {
        self.nameField.enabled=NO;
        self.phoneField.enabled=NO;
        [self.view endEditing:YES];
        self.saveBtn.hidden=YES;
        item.title=@"编辑";

        //还原原来的数据
        self.nameField.text=self.contact.name;
        self.phoneField.text=self.contact.phone;
    }else {
        self.nameField.enabled = YES;
        self.phoneField.enabled = YES;
        [self.phoneField becomeFirstResponder];
        self.saveBtn.hidden = NO;

        item.title = @"取消";

    }
}

/**
 *  保存
 */
- (IBAction)save {
    //1.关闭页面
    [self.navigationController popViewControllerAnimated:YES];

    //2.通知代理
    if ([self.delegate respondsToSelector:@selector(editViewController:didSaveContact:)]) {
        //更新模型数据
        self.contact.name=self.nameField.text;
        self.contact.phone=self.phoneField.text;
        [self.delegate editViewController:self didSaveContact:self.contact];
    }
}
@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS开发 - 私人通讯录实例

标签:通讯录   ios开发   uiviewcontroller   mvc   

原文地址:http://blog.csdn.net/wangzi11322/article/details/47354987

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