iOS protocol传值

  一 、

        1、直接在需要传递数据的文件种声明protocol。

              A.h//需要传递数据的文件

     @property(retain,nonatomic)id<FinanceStreetDelegate> delegate; 

      @end

      @protocol FinanceStreetDelegate <NSObject>

      -(void)showCreditInfo:(NSString*)userId;

      @end

     A.m中协议的方法,通过参数传递数据

        [self.delegate showCreditInfo:@"23"];

  2、在需要接收数据的文件中实现这个协议 

      B.h中

                  @interface b : UIViewController <FinanceStreetDelegate>

      B.m中 

      A *aa=[[A alloc] init];

       aa.delegate=self;

       -(void)showCreditInfo:(NSString*)userId{
        //userId 就是传过来的值。 
      }  
 
 
二 、直接创建一个protocol文件。
       

 1、创建protocol

#import <Foundation/Foundation.h>

 //PopTableAction.h

@protocol PopTableAction <NSObject>

-(void)doTableResult:(NSString*)resultInfo;

@end

2、传递数据

     A.h

#import "PopTableAction.h"

@interface A : UIViewController

@property (nonatomic,assign) id<PopTableAction>delegate;

@end

A.m中//比如在tableview的方法中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

         NSString *selectedCellText =@"sa";

          [self.delegate doTableResult:selectedCellText];

}

2、实现protocol,取得数据
   B.h

@interface B : UIViewController<PopTableAction

 
  B.m中;
   A *a;
  a.delegate=self;
  

-(void)doTableResult:(NSString*)resultInfo{

}

  
原文地址:https://www.cnblogs.com/sgdkg/p/2715427.html