您的位置:首页 > 产品设计 > UI/UE

iOS开发UI篇—实现一个私人通讯录小应用(二)

2014-11-02 21:48 323 查看
iOS开发UI篇—实现一个私人通讯录小应用(二)

一、实现功能说明

(1)点击注销按钮,弹出一个对话框,点击确定后移除当前栈顶的控制器,返回开始界面,点击取消,不做任何操作。

注意:注销按钮的单击事件已经进行了连线。实现-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex需要遵守UIActionSheetDelegate协议。

1 //注销按钮
2 - (IBAction)logoutBtn:(id)sender {
3
4     UIActionSheet *sheet =[[UIActionSheet alloc]initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: nil];
5
6     [sheet showInView:self.view];
7 }
8
9 #pragma mark-代理方法
10 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
11 {
12     if (buttonIndex!=0)return;
13     //移除栈顶的控制器
14     [self.navigationController popViewControllerAnimated:YES];
15 }


(2)当两个文本框的状态发生改变时,通知添加按钮变为可用状态。

知识点:通知(注册监听)

1 - (void)viewDidLoad
2 {
3     [super viewDidLoad];
4
5     //1.获得通知中心
6     NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
7     //2.注册监听
8     [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameFeild];
9     [center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneFeild];
10 }
11
12
13
14 //当文本框内容改变的时候,通知self调用该方法
15 -(void)textChange
16 {
17     //判断,如果两个文本框的内容都改变(有值)的时候,添加按钮变成可交互的
18     self.addBtn.enabled=(self.nameFeild.text.length>0&&self.phoneFeild.text.length>0);
19     NSLog(@"通知调用的事件");
20 }
21
22 //临终遗言
23 -(void)dealloc
24 {
25     [[NSNotificationCenter defaultCenter] removeObserver:self];
26 }


(3)数据的逆传(使用代理)

TXContatcsViewController.m文件

//  Created by 鑫 on 14-10-21.
//  Copyright (c) 2014年 梁镋鑫. All rights reserved.
//

#import "TXContactsViewController.h"
#import "TXAddViewController.h"
#import "TXContact.h"
#import "TXEditViewController.h"
#import "TXContactCell.h"

@interface TXContactsViewController ()<UIActionSheetDelegate,TXAddViewControllerDelegate, TXEditViewControllerDelegate>
- (IBAction)logout:(id)sender;
@property (nonatomic, strong) NSMutableArray *contacts;
@end

@implementation TXContactsViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (NSMutableArray *)contacts
{
if (_contacts == nil) {
_contacts = [NSMutableArray array];
}
return _contacts;
}

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

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return self.contacts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
TXContactCell *cell = [TXContactCell cellWithTableView:tableView];

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

return cell;
}

/**
*  注销
*
*  @param sender <#sender description#>
*/

- (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) {
//        //出栈
//        [self.navigationController popViewControllerAnimated:YES ];
//    }
if (buttonIndex !=0) return;
//出栈
[self.navigationController popViewControllerAnimated:YES ];

}
/**
*  执行跳转下个控制器之前会调用下面这个方法
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id vc = segue.destinationViewController;

if ([vc isKindOfClass:[TXAddViewController class]]) { // 如果是跳转到添加联系人的控制器
// 设置下一个控制器(添加联系人的控制器)的代理
TXAddViewController *addVc = vc;
addVc.delegate = self;
} else if ([vc isKindOfClass:[TXEditViewController class]]) { // 如果是跳转到查看(编辑)联系人的控制器
TXEditViewController *editVc = vc;
// 取得选中的那行
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//传递数据给editVC

editVc.contact = self.contacts[path.row];
editVc.delegate = self;
}

}

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

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


TXAddViewController.h文件

#import <UIKit/UIKit.h>
//包括TXContact这个模型
@class TXAddViewController, TXContact;

@protocol TXAddViewControllerDelegate <NSObject>

@optional
//- (void)addViewController:(TXAddViewController *)addVc didAddContactWithName:(NSString *)name phone:(NSString *)phone;你当我代理你就能拿到我那两个属性值,如果参数多,不好用,可用模型,以下使用模型的
- (void)addViewController:(TXAddViewController *)addVc didAddContact:(TXContact *)contact;
@end

@interface TXAddViewController : UIViewController
@property (nonatomic, weak) id<TXAddViewControllerDelegate> delegate;
@end


TXAddViewController.m文件

//  Created by 鑫 on 14-10-21.
//  Copyright (c) 2014年 梁镋鑫. All rights reserved.
//

#import "TXAddViewController.h"
#import "TXContact.h"
@interface TXAddViewController ()
//为了封装性不要把控件属性放到.h文件
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
- (IBAction)add;
@end

@implementation TXAddViewController

- (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.传递数据给上一个控制器(TXContactsViewController)
// 2.通知代理
if ([self.delegate respondsToSelector:@selector(addViewController:didAddContact:)]) {//代理还可以传值
TXContact *contact = [[TXContact alloc] init];
contact.name = self.nameField.text;
contact.phone = self.phoneField.text;
[self.delegate addViewController:self didAddContact:contact];
}
}

@end


TXEditViewController.h文件

//  Created by 鑫 on 14-10-21.
//  Copyright (c) 2014年 梁镋鑫. All rights reserved.
//

#import <UIKit/UIKit.h>
@class TXContact, TXEditViewController;

@protocol TXEditViewControllerDelegate <NSObject>

@optional
- (void)editViewController:(TXEditViewController *)editVc didSaveContact:(TXContact *)contact;

@end

@interface TXEditViewController : UIViewController
//别人传一个模型给你
@property (nonatomic, strong) TXContact *contact;

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


TXEditViewController.m文件

//  Copyright (c) 2014年 梁镋鑫. All rights reserved.
//

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

- (IBAction)save;

@end

@implementation TXEditViewController
- (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


二、效果



注销的弹窗效果



添加信息



信息添加后返回到联系人列表界面

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐