您的位置:首页 > 其它

控制器之间的数据传递——代理传值

2016-08-29 18:07 211 查看

代理传值

我们先在这里约定:界面1传值到界面2为顺传,界面2传值到界面1为逆传

一般用代理传值是为了降低视图控制器之间的耦合度,主要用于逆序传值。

对于属性传值,传递方必须要有一个保存接收方的属性,而代理传值不需要知道接收方是谁,只要其代理遵守代理协议,就可以获取通过代理方法传出的数据模型

一. 实现步骤

界面2实现一个代理协议

界面2设置一个代理属性

界面2通知其代理做事情

界面2在入栈之前,设置界面1为其代理

界面1实现代理方法,实现传值

二. 具体代码

1. AppDelegate类

---------- AppDelegate.m文件

#import "AppDelegate.h"
#include "OneViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 1. 创建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

// 2. 创建窗口的根控制器
// 2.1 创建导航控制器的根控制器
UIViewController *oneVc = [[OneViewController alloc] init];
// 2.2 创建导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:oneVc];
// 2.3 给窗口设置根控制器
self.window.rootViewController = nav;

// 3. 设置窗口为主窗口并显示窗口
[self.window makeKeyAndVisible];

// 隐藏导航控制器的导航条
nav.navigationBarHidden = YES;

return YES;
}

@end


2. OneViewController类

---------- OneViewController.m文件

#import "OneViewController.h"
#import "TwoViewController.h"

@interface OneViewController ()<TwoViewControllerDelegate>

@property (nonatomic,strong) UITextField *textField;

@end

@implementation OneViewController

- (void)viewDidLoad {
[super viewDidLoad];

//设置控制器View的背景颜色
self.view.backgroundColor = [UIColor greenColor];

// 创建点击按钮
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clickBtn.frame = CGRectMake(10, 100, 80, 40);
clickBtn.backgroundColor = [UIColor whiteColor];
[clickBtn setTitle:@"到界面2" forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickBtn];

// 创建文本框
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];

}

#pragma mark - 点击事件

- (void)btnClick {

// 创建界面2,并压入栈
TwoViewController *twoVc = [[TwoViewController alloc] init];
[self.navigationController pushViewController:twoVc animated:YES];

// 设置界面2的代理
twoVc.delegate = self;

}

#pragma mark - 实现代理方法

- (void)twoViewController:(TwoViewController *)vc andString:(NSString *)str {
_textField.text = str;
}

@end


3. TwoViewController类

---------- TwoViewController.h文件

#import <UIKit/UIKit.h>

#pragma mark - TwoViewController代理协议
@class TwoViewController;

@protocol TwoViewControllerDelegate <NSObject>

@optional
- (void)twoViewController:(TwoViewController *) vc andString:(NSString *)str;

@end

#pragma mark - TwoViewController控制器声明

@interface TwoViewController : UIViewController

// 代理
@property (nonatomic,weak) id<TwoViewControllerDelegate> delegate;

@end

---------- TwoViewController.m文件

#import "TwoViewController.h"

@interface TwoViewController ()

@property (nonatomic,strong) UITextField *textField;

@end

@implementation TwoViewController

- (void)viewDidLoad {
[super viewDidLoad];

//设置控制器View的背景颜色
self.view.backgroundColor = [UIColor yellowColor];

// 创建点击按钮
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
clickBtn.frame = CGRectMake(10, 100, 80, 40);
clickBtn.backgroundColor = [UIColor whiteColor];
[clickBtn setTitle:@"传 值" forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clickBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickBtn];

// 创建文本框
_textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 150, 250, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_textField];
}

#pragma mark - 点击事件

- (void)btnClick {

// 通知代理做事情
if ([_delegate respondsToSelector:@selector(twoViewController:andString:)]) {
[_delegate twoViewController:self andString:_textField.text];
}

// 跳转界面
[self.navigationController popViewControllerAnimated:YES];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息