ViewControllerB.h文件:
#import // 新建一个协议,协议的名字一般是由“类名+Delegate”
@protocol ViewControllerBDelegate // 代理传值方法
- (void)sendValue:(NSString *)value;
@end
@interface ViewControllerB : UIViewController
// 委托代理人,代理一般需使用弱引用(weak)
@property (weak, nonatomic) id delegate;
@end
ViewControllerB.m文件:
#import "ViewControllerB.h"
@interface ViewControllerB ()
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewControllerB
- (IBAction)backAction:(id)sender
{
if ([_delegate respondsToSelector:@selector(sendValue:)]) { // 如果协议响应了sendValue:方法
[_delegate sendValue:_textField.text]; // 通知执行协议方法
}
[self.navigationController popViewControllerAnimated:YES];
}
@end
页面遵循代理,并且实现代理方法即可
君凯商联网-iOS-字唐名僧