代理传值
代理传值一般用于逆向传值,即第二个页面传值给第一个页面
ViewController页面push到SecondViewController页面,如果SecondViewController页面的信息想回传(回调)到ViewController页面,用代理传值,其中SecondViewController定义协议和声明代理,ViewController确认并实现代理,ViewController作为SecondViewController的代理
- 第一步
- 首先新建一个SecondViewController,并且在SecondViewController里面
- 定义协议
- 设置协议中的方法
- 声明代理
- 第二步
- 在SecondViewController.m中为其绑定相应方法
- 第三步
- ViewController.m中
- 设置代理
- 服从协议
- 实现代理方法
下面我们上代码
1.在SecondViewController.h中创建协议方法
import <UIKit/UIKit.h>
//首先在SecondViewController.h中创建协议方法
@class SecondViewController;
//定义协议
@protocol PushValueDelegate <NSObject>
//设置协议中的方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info;
@end
@interface SecondViewController : UIViewController
//声明代理
@property (nonatomic,strong)id<PushValueDelegate>delagate;
@end
2.在SecondViewController.m文件中
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)NSString *textString;
@end
@implementation SecondViewController
-(void)viewWillDisappear:(BOOL)animated{
// 设置视图将要消失时.通过代理传值
// 判断代理是否存在,并且设置代理能够响应代理方法时,才执行代理方法
if (self.delagate && [self.delagate respondsToSelector:@selector(viewController:didPushValueWithInfo:)]) {
[self.delagate viewController:self didPushValueWithInfo:_textField.text];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textField.backgroundColor = [UIColor whiteColor];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
}
3.回到我们的ViewController.m中,首先我们需要导入一下头文件,然后在遵循在SecondViewController里面声明的协议,最后在viewDidLoad里面创建UITextField 和 UIButton
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<PushValueDelegate>
@property (nonatomic,strong) UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"代理传值";
self.view.backgroundColor = [UIColor yellowColor];
/**
代理传值(场景)一般用于逆向传值,即第二个界面传值给第一个界面
ViewController页面push到SecondViewController页面,如果SecondViewController页面的信息想回传(回调)到ViewController页面,用代理传值,其中SecondViewController定义协议和声明代理,ViewController确认并实现代理,ViewController作为SecondViewController的代理
第一步:首先,在SecondViewController.h中
(1).定义协议
(2).设置协议中的方法
(3).声明代理
然后,在SecondViewController.m中
(1).为其绑定相应方法
第二步:在FirstViewController.m中
(1).设置代理
(2).服从协议
*/
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)];
_textField.backgroundColor = [UIColor whiteColor];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(150, 200, 80, 44);
[btn setTitle:@"Next" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
4.实现一下Button里面的点击事件方法,并设置代理
-(void)nextAction:(UIButton *)sender{
SecondViewController *secondeVC = [[SecondViewController alloc]init];
// 设置代理
secondeVC.delagate = self;
[self.navigationController pushViewController:secondeVC animated:YES];
}
5.实现代理方法
//实现代理方法
-(void)viewController:(SecondViewController *)ViewController didPushValueWithInfo:(id)info{
_textField.text = info;
}
6.由于是通过navigationController跳转到SecondViewController里面的,所以我们需要在AppDelegate里面创建一个UINavigationController
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
[self.window makeKeyAndVisible];
return YES;
}
7.模拟器运行效果如下