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

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

2014-11-13 12:21 567 查看
一、实现功能说明

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

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

//注销按钮
- (IBAction)logoutBtn:(id)sender {

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

[sheet showInView:self.view];
}

#pragma mark-代理方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex!=0)return;
//移除栈顶的控制器
[self.navigationController popViewControllerAnimated:YES];
}


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

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

- (void)viewDidLoad
{
[super viewDidLoad];

//1.获得通知中心
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
//2.注册监听
[center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameFeild];
[center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneFeild];
}

//当文本框内容改变的时候,通知self调用该方法
-(void)textChange
{
//判断,如果两个文本框的内容都改变(有值)的时候,添加按钮变成可交互的
self.addBtn.enabled=(self.nameFeild.text.length>0&&self.phoneFeild.text.length>0);
NSLog(@"通知调用的事件");
}

//临终遗言
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}


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

YYContatcsViewController.m文件

//
//  YYContatcsViewController.m
//  01-私人通讯录(登录页面搭建)
//
//  Created by apple on 14-6-8.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYContatcsViewController.h"
#import "YYAddViewController.h"
#import "YYInfoModel.h"

//遵守协议
@interface YYContatcsViewController ()<UIActionSheetDelegate,YYAddViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;

//数组,用来保存用户添加的数据
@property(nonatomic,strong)NSMutableArray *array;

- (IBAction)logoutBtn:(id)sender;

@end

@implementation YYContatcsViewController

- (void)viewDidLoad
{
[super viewDidLoad];
}

//注销按钮
- (IBAction)logoutBtn:(id)sender {

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

[sheet showInView:self.view];
}

#pragma mark-代理方法
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex!=0)return;
//移除栈顶的控制器
[self.navigationController popViewControllerAnimated:YES];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *c=segue.destinationViewController;
YYAddViewController *addc=(YYAddViewController *)c;
addc.delegate=self;

}

#pragma mark-YYAddViewControllerDelegate
-(void)addViewControllerDidAddBtn:(YYAddViewController *)addViewController contatc:(YYInfoModel *)contatc
{
//1.把数组保存到数组中
//    [self.Info addObject:contatc];
//    //2.刷新表格
//    NSLog(@"%@",contatc);
//    NSLog(@"%@",self.Info);
//    [self.tableview reloadData];
NSLog(@"%@,%@",contatc.name,contatc.phone);
[self.array addObject:contatc];
[self.tableview reloadData];

}

-(NSMutableArray *)array
{
if (_array==Nil) {
_array=[NSMutableArray array];
}
return _array;
}
#pragma mark-tableview的数据源
//一共有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//       return self.Info.count;
return self.array.count;
}
//每组每行的cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"info";
//先去缓存中取。如果缓存中没有,那么就到storyboard中去查找
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//在storyboard中设置cell的标识符为info

//设置cell的数据
//
//     UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Nil];

YYInfoModel *info=self.array[indexPath.row];
cell.textLabel.text=info.name;
cell.detailTextLabel.text=info.phone;

//返回cell
return cell;
}
@end


YYAddViewController.h文件

//
//  YYAddViewController.h
//  01-私人通讯录(登录页面搭建)
//
//  Created by 孔医己 on 14-6-8.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import <UIKit/UIKit.h>
@class YYAddViewController, YYInfoModel;
//自定义一个协议,让上一个控制器(YYContatcsViewController)成为当前控制器的代理

//@protocol  YYAddViewControllerDelegate <NSObject>
//
////协议方法
//-(void)addViewControllerDidAddBtn:(YYAddViewController *)addViewController contatc:(YYInfoModel *)contatc;

@protocol YYAddViewControllerDelegate <NSObject>

//- (void)editViewControllerDidAddBtn:(NJEditViewController *)editViewController name:(NSString *)name number:(NSString *)phoneNumber;

- (void)addViewControllerDidAddBtn:(YYAddViewController *)editViewController contatc:(YYInfoModel *)contatc;

@end

@interface YYAddViewController : UIViewController

//新增一个代理属性
@property(nonatomic,strong)id<YYAddViewControllerDelegate> delegate;

@end


YYAddViewController.m文件

//
//  YYAddViewController.m
//  01-私人通讯录(登录页面搭建)
//
//  Created by 孔医己 on 14-6-8.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYAddViewController.h"
#import "YYInfoModel.h"

@interface YYAddViewController ()
//姓名输入框
@property (weak, nonatomic) IBOutlet UITextField *nameFeild;
//电话号码输入框
@property (weak, nonatomic) IBOutlet UITextField *phoneFeild;
//添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addBtn;

//添加按钮的点击事件
- (IBAction)addBtnOnclick:(id)sender;

@end

@implementation YYAddViewController

#pragma mark- 监听是否添加
- (void)viewDidLoad
{
[super viewDidLoad];

//1.获得通知中心
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
//2.注册监听
[center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameFeild];
[center addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.phoneFeild];
}

//当文本框内容改变的时候,通知self调用该方法
-(void)textChange
{
//判断,如果两个文本框的内容都改变(有值)的时候,添加按钮变成可交互的
self.addBtn.enabled=(self.nameFeild.text.length>0&&self.phoneFeild.text.length>0);
NSLog(@"通知调用的事件");
}

//临终遗言
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidAppear:(BOOL)animated
{
// 3.主动召唤出键盘
[self.nameFeild becomeFirstResponder];
//    [self.nameField resignFirstResponder];
}

- (IBAction)addBtnOnclick:(id)sender {

//点击添加按钮后,把数据保存到模型数组中
//跳转
[self.navigationController popViewControllerAnimated:YES];

//1.把当前界面文本框中的信息保存到模型中
YYInfoModel *info=[[YYInfoModel alloc]init];
info.name=self.nameFeild.text;
info.phone=self.phoneFeild.text;

//2.数据逆传(把当前控制器view的数据传递到上一个控制器的view中)
//使用代理,自定义一个代理,并使用代理传递数据
//如果代理方法存在就通知代理调用该方法,传递数据
if ([self.delegate respondsToSelector:@selector(addViewControllerDidAddBtn:contatc:)]) {
NSLog(@"sadafaf");
[self.delegate addViewControllerDidAddBtn:self contatc:info];
}

NSLog(@"dddd");
}
@end


二、效果

注销的弹窗效果



添加信息



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

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