协议传值

上一篇文章介绍了属性的从前向后传值 从后向前传值 但是在实际应用中 从后向前传值一般用协议传值不用属性传值
下面要我们来了解一下什么事属性传值

我们从程序中来学习

// FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end


// FirstViewController.m

#import "FirstViewController.h"
#import "SecViewController.h"
// 签订协议
@interface FirstViewController () <passStr>
@property (nonatomic, strong) UILabel *label;

@end

@implementation FirstViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent   = NO;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
self.label.backgroundColor = [UIColor greenColor];
self.label.textAlignment = 1;
[self.view addSubview:self.label];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 50, 100, 30);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}

- (void)click:(UIButton *)button {
SecViewController *secVC = [[SecViewController alloc] init];
// 指定代理人
secVC.delegate = self;
[self.navigationController pushViewController:secVC animated:YES];
}
// 实现协议方法
- (void)passValue:(NSString *)str {
self.label.text = str;
}

// SecViewController.h

#import <UIKit/UIKit.h>
// 声明协议
@protocol passStr <NSObject>
// 声明协议方法
- (void)passValue:(NSString *)str;

@end

@interface SecViewController : UIViewController
@property (nonatomic, copy) NSString *labelTitle;
// 声明代理人
@property (nonatomic, weak) id <passStr> delegate;

@end

// SecViewController.m

#import "SecViewController.h"

@interface SecViewController ()
@property (nonatomic, strong) UITextField *textField;

@end

@implementation SecViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = [UIColor yellowColor];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 180, 30)];
self.textField.backgroundColor = [UIColor redColor];
//
self.textField.text = self.labelTitle;
[self.view addSubview:self.textField];

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(50, 100, 100, 30);
backButton.backgroundColor = [UIColor greenColor];
[backButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
NSLog(@"%@",   self.navigationController.viewControllers);
}

- (void)click:(UIButton *)button {
// 触发协议方法
[self.delegate passValue:self.textField.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}

协议传值一共分为6步(从后往前传值)

  1. 声明协议 (在第二页的.h中进行协议的声明 还包括协议方法的声明)
  2. 声明代理人 (在第二页的.h中)
  3. 签订协议 (在第一页的.m中)
  4. 指定代理人 (在第一页的.m中点击方法中)
  5. 实现协议方法 (在第一页的.m中)
  6. 触发协议方法 (在第二页的.m点击方法中)

推荐阅读更多精彩内容