iOS开发 - 私人通讯录实例

私人通讯录实例

此实例主要练习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 [email protected]"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[email protected]"编辑";

        //还原原来的数据
        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

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

时间: 2024-12-24 16:20:24

iOS开发 - 私人通讯录实例的相关文章

iOS开发-私人通讯录-数据存储和编辑模式

UI界面 -(void)viewDidLoad{ [super viewDidLoad]: //addTarget:一般用于监听按钮的点击 以及进度条值的改变 //通过通知监听UITextField的改变 /* addObserver:谁来监听 selector:通知发生的时候调用什么方法 name:通知名称 object:谁发送的通知 注意:object不能写nil,因为如果是nil只要是UITextField发生改变都会调用textChange方法,而我们只在账号和密码输入框发生改变的时候才

iOS开发之--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook功能开发汇总

前言 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 目录 系统应用 系统服务 2.1. 短信与邮件 2.2. 通讯录 2.3. 蓝牙 2.4. 社交 2.5. Game Center 2.6. 应用内购买 2.7. iCloud 2.8. Passbook 1. 系统应用 在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览

iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 调用系统应用 使用系统服务 短信与邮件 通讯录 蓝牙 社交 Game Center 应用内购买 iCloud Passbook 系统应用 在开发某些应用时可能希望能够调用iOS系统内置的电话.短信.邮件.浏览器应用,此时你可以直接使用UIApplication的OpenUR

iOS开发系列通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开

--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务:http://www.jinhusns.com/Products/Download/?type=xcj 调用系统应用 使用系统服务 短信与邮件 通讯录 蓝牙 社交 Game Center 应用内购买 iCloud Passbook 目 录 系统应用 在开发某些应用时可能希望能

iOS开发长文--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

链接:http://www.cocoachina.com/ios/20150129/11068.html iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: 调用系统应用 使用系统服务 短信与邮件 通讯录 蓝牙 社交 Game Center 应用内购买 iCloud Passbook 系统应用 在开发某些应用时可能希望能够调用iOS系统内置的电话.

iOS开发之通讯录 AddressBook

如何访问用户的通讯录 在iOS中,有2个框架可以访问用户的通讯录 AddressBookUI.framework 提供了联系人列表界面.联系人详情界面.添加联系人界面等一般用于选择联系人 AddressBook.framework 纯C语言的API,仅仅是获得联系人数据 没有提供UI界面展示,需要自己搭建联系人展示界面 里面的数据类型大部分基于Core Foundation框架 从iOS6开始,需要得到用户的授权才能访问通讯录,因此在使用之前,需要检查用户是否已经授权 获得通讯录的授权状态:AB

iOS开发--系统通讯录的访问与添加联系人

公司项目有访问通讯录的需求,所以开始了探索之路.从开始的一无所知,到知识的渐渐清晰.这一切要感谢广大无私分享的 “coder”,注:我是尊称的语气! 苹果提供了访问系统通讯录的框架,以便开发者对系统通讯录进行操作.(此demo为纯代码),想要访问通讯录,需要添加AddressBookUI.framework和AddressBook.framework两个框架,添加的地点这里就不在赘述了.在控制器内部首先import两个头文件,<AddressBook/AddressBook.h> 和 <

iOS开发------操作通讯录(AddressBook篇)&amp;通讯录UI(AddressBookUI篇)

上篇博文简要的介绍了如何使用AddessBook.framework来获取系统通讯录,但有时候又想对其做修改怎么办,那么这篇博文就总结一下如何修改系统的通讯录吧. 代码GitHub:https://github.com/YRunIntoLove/YAddressBookUIDemo 修改系统通讯录的方法 两种方法 通过AddressBook.framework的各种函数来完成对AddressBook的操作. 通过AddressBookUI.framework中提供的系统UIViewControl

IOS开发-图片浏览器实例-UIImageView的使用-懒加载-plist文件的使用

一.本文概述 一个使用UIImageView.UILabel.UIButton实现的图片浏览器的实例,界面如图:   功能描述: 1. 点击左右箭头切换图片.图片描述.图片序号: 2.第一张图片时左箭头不能点击 3.最后一张图片时右箭头不能点击 4.点击设置按钮出现一个可设置的界面(上图中黄色背景的部分)可以设置模式和对图片的缩放 实现概述: 1.搭建UI界面,使用UIImageView控件显示图片 2. 监听个按钮的点击 3. 切换图片内容,描述,序号.背景色及UIImageView的tran