首页 > 代码库 > 代理传值(B->A)

代理传值(B->A)

从BViewController传到AViewController

BViewController.h

#import <UIKit/UIKit.h>

@protocol IDDelegate <NSObject>

- (void)setIDValue:(NSString *)value;

@end

@interface BViewController : UIViewController

@end

 

BViewController.m

#import "BViewController.h"

@interface BViewController ()

@property(nonatomic, retain) id<IDDelegate> idDelegate; // 注册一个代理

@end

然后在需要跳转的时候
    AViewController *a = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];

    self.idDelegate = a; // 这里和AViewController 进行交互,下一句代码就是对应的赋值

    [self.idDelegate setIDValue:stringValue];

    [self.navigationController popToViewController:qvc animated:true];

 

AViewController.h

#import "BViewController.h" // 导入协议

@interface AViewController : UIViewController<IDDelegate>// 遵守协议

@property (strong, nonatomic) UITextField *contactIdField;

- (void)setIDValue:(NSString *)value;

@end

AViewController.m

- (void)setIDValue:(NSString *)value // 实现方法

{

    self.contactIdField.text = value ;

}

 

代理传值(B->A)