今天简单的向大家介绍一下属性传值相关的内容 众所周知可以利用属性从前一个页面向后一个页面进行传值, 而从后一个页面向前一个页面传值一般使用协议传值 今天我们使用属性从后往前传值
我们来创建一个简单的工程: 创建一个FirstViewController和一个SecViewController 在FirstViewController中创建一个button和一个label 点击button可以push到SecViewController 并且将label的标题传给textfield.text 在SecViewController中创建一个button和一个textField 点击button可以pop回FirstViewController 并可将textfield中输入的内容传给FirstViewController中label.text
// FirstViewController.h
// 从前向后传值
#import
@interface FirstViewController : UIViewController
@property (nonatomic, copy) NSString *labelText;
@end
// FirstViewController.m
#import "FirstViewController.h"
#import "SecViewController.h"
@interface FirstViewController ()
@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.text = @"每一天";
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.labelTitle = self.label.text;
[self.navigationController pushViewController:secVC animated:YES];
}
// SecViewController.h
#import
#import "FirstViewController.h"
@class FirstViewController;
@interface SecViewController : UIViewController
@property (nonatomic, copy) NSString *labelTitle;
@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);
}
// 解释一下: 在SecViewController的.h文件中创建一个textField.text的属性 在FirstViewController中的.m文件 引入SecViewController头文件 在FirstViewController的button点击方法中创建SecViewController对象 在利用这个对象"."出对应的属性 将FirstViewController中的self.label.text赋值给这个属性 当单机button时 完成属性的从前向后的传值
利用属性从后向前传值
// FirstViewController.h
#import
@interface FirstViewController : UIViewController
@property (nonatomic, copy) NSString *labelText;
@end
// FirstViewController.m
#import "FirstViewController.h"
#import "SecViewController.h"
@interface FirstViewController ()
@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.labelTitle = self.label.text;
[self.navigationController pushViewController:secVC animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
self.label.text = self.labelText;
}
// SecViewController.h
#import
#import "FirstViewController.h"
@class FirstViewController;
@interface SecViewController : UIViewController
@property (nonatomic, copy) NSString *labelTitle;
@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 {
NSInteger num = self.navigationController.viewControllers.count;
FirstViewController *first = self.navigationController.viewControllers[num - 2];
first.labelText = self.textField.text;
[self.navigationController popToRootViewControllerAnimated:YES];
}
// 解释一下 属性的从后向前传值就是在后一页pop到前一页的点击方法实现 先要获得第一页的对象 再给对象的属性进行赋值 NSInteger num = self.navigationController.viewControllers.count;
FirstViewController *first = self.navigationController.viewControllers[num - 2];
first.labelText = self.textField.text;
(这里要通过self.navigationController.viewControllers这个数组来得到前一页的VC)
// 总之想要完成属性传值 就要活得响应的VC