上一篇文章介紹了屬性的從前向后傳值 從后向前傳值 但是在實際應用中 從后向前傳值一般用協(xié)議傳值不用屬性傳值
下面要我們來了解一下什么事屬性傳值
我們從程序中來學習
// FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@end
// FirstViewController.m
#import "FirstViewController.h"
#import "SecViewController.h"
// 簽訂協(xié)議
@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];
}
// 實現(xiàn)協(xié)議方法
- (void)passValue:(NSString *)str {
self.label.text = str;
}
// SecViewController.h
#import <UIKit/UIKit.h>
// 聲明協(xié)議
@protocol passStr <NSObject>
// 聲明協(xié)議方法
- (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 {
// 觸發(fā)協(xié)議方法
[self.delegate passValue:self.textField.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
協(xié)議傳值一共分為6步(從后往前傳值)
- 聲明協(xié)議 (在第二頁的.h中進行協(xié)議的聲明 還包括協(xié)議方法的聲明)
- 聲明代理人 (在第二頁的.h中)
- 簽訂協(xié)議 (在第一頁的.m中)
- 指定代理人 (在第一頁的.m中點擊方法中)
- 實現(xiàn)協(xié)議方法 (在第一頁的.m中)
- 觸發(fā)協(xié)議方法 (在第二頁的.m點擊方法中)