协议的实现

正向传值,反向传值

正传用Segue,反传用代理协议

反向传值如需从ContactTableViewController中点击添加按钮跳转到EditViewController,添加完又返回到ContactTableViewController;

只要点击就跳转的segue是自动的,(按钮与下一个视图连接)点击之后判断正确登陆的是手动的segue(视图与视图连接)

EditViewController.hzhong

//定义协议后有警告,错误时强行转换

@class EditViewController,Contatc;

//声明协议

@protocol EditViewControllerDelegate <NSObject>

- (void)editViewControllerDidAddBtn:(EditViewController  *)editViewController contatc:(Contatc * )contatc;


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


建立一个model文件夹,创建一个Contact (  Object )类,方面直接调用,以后修改方便

在头文件中写

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *phoneNumber; 



//保存所有用户数据 数据是可变的用strong

@property(nonatomic,strong)NSMutableArray * constatcs;


EditViewController.m中

- (IBAction)addBtnClick:(UIButton *)sender {

    

    //0.移除栈顶控制器

    [self.navigationControllerpopViewControllerAnimated:YES];

    //1.获取用户输入的姓名和电话

   NSString * name = self.nameField.text;

   NSString * phone = self.phoneField.text;

    

   Contatc * c = [[Contatcalloc] init];

    c.name = name;

    c.phoneNumber = phone;

    

    //2.传递数据给联系人列表

   if ([self.delegaterespondsToSelector:@selector(editViewControllerDidAddBtn:contatc:)]) {

        [self.delegateeditViewControllerDidAddBtn:selfcontatc:c];

    }

    

}


ContactTableViewController.m中添加                                  

#import "EditViewController.h"

#import "Contatc.h"

在@interface ContatcsTableViewController () <UIActionSheetDelegate>里面添加EditViewControllerDelegate

@interface ContatcsTableViewController () <UIActionSheetDelegate,EditViewControllerDelegate>


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    //1.取出目标控制器

    EditViewController * editVc = (EditViewController *)segue.destinationViewController;

    //2.设置代理

    editVc.delegate =self;

}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //NSLog(@"%d",buttonIndex);

    //等同于下面两句代码

   if (0 == buttonIndex) {

        //移除栈顶控制器

        [self.navigationControllerpopViewControllerAnimated:YES];

    }

    

//    if (0 != buttonIndex) return;

//    [self.navigationController popViewControllerAnimated:YES];

    

}

#pragma  - EditViewControllerDelegate

-(void)editViewControllerDidAddBtn:(EditViewController *)editViewController contatc:(Contatc *)contatc

{

   NSLog(@"添加了新的联系人 %@ %@", contatc.name,contatc.phoneNumber);

    //1.保存数据到数组中

    [self.constatcsaddObject:contatc];

    //2.刷新表格

    [self.tableViewreloadData];

}


#pragma mark - 数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.constatcs.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString * identifier =@"constatcs";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:identifier];

    }

    //设置数据

   Contatc * c = self.constatcs[indexPath.row];

    cell.textLabel.text = c.name;

    cell.detailTextLabel.text = c.phoneNumber;

    //返回cell

   return cell;

}


-(NSMutableArray * )constatcs

{

   if (_constatcs ==nil) {

       _constatcs = [NSMutableArrayarray];

    }

    return_constatcs;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值