協議的實現

正向傳值,反向傳值

正傳用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;

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章