iOS开发中,页面传值是很常见的,但是页面传值你究竟知道多少呢?笔者这篇文章就是给大家介绍一下页面传值的具体方式,有不足之处,欢迎大家指正,希望能和大家共同进步。说明一下:这里所说的正向、反向传值是指相关联的两个页面间的传值。
属性传值
用法:正向传值
需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController ()
@property (nonatomic, strong) UITextField *aTextField;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.aTextField.layer.borderWidth = 1;
[self.view addSubview:self.aTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
B_ViewController *bViewController = [[B_ViewController alloc] init];
bViewController.string = self.aTextField.text;
[self.navigationController pushViewController:bViewController animated:YES];
}
@end
B控制器.h文件:
#import <UIKit/UIKit.h>
@interface B_ViewController : UIViewController
@property (nonatomic, copy) NSString *string;
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *bLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
bLabel.layer.borderColor = [UIColor grayColor].CGColor;
bLabel.layer.borderWidth = 1;
[self.view addSubview:bLabel];
bLabel.text = self.string;
}
@end
代理传值
用法:反向传值:
需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController () <BToADelegate>
@property (nonatomic, strong) UILabel *aLabel;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
self.aLabel.layer.borderWidth = 1;
[self.view addSubview:self.aLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"push到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)buttonAction:(UIButton *)sender {
B_ViewController *bViewController = [[B_ViewController alloc] init];
bViewController.delegate = self;
[self.navigationController pushViewController:bViewController animated:YES];
}
- (void)transferString:(NSString *)string {
self.aLabel.text = string;
}
@end
B控制器.h文件:
#import <UIKit/UIKit.h>
@protocol BToADelegate <NSObject>
- (void)transferString:(NSString *)string;
@end
@interface B_ViewController : UIViewController
@property (nonatomic, weak) id<BToADelegate> delegate;
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@property (nonatomic, strong) UITextField *bTextField;;
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.bTextField.layer.borderWidth = 1;
[self.view addSubview:self.bTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(transferString:)]) {
[self.delegate transferString:self.bTextField.text];
}
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
Block传值
用法:反向传值:
需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController ()
@property (nonatomic ,strong) UILabel *aLabel;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
self.aLabel.layer.borderWidth = 1;
[self.view addSubview:self.aLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"push到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
B_ViewController *bViewController = [[B_ViewController alloc] init];
__weak __typeof(self) weakSelf = self;
[bViewController setBlock:^(NSString *string){
weakSelf.aLabel.text = string;
}];
[self.navigationController pushViewController:bViewController animated:YES];
}
B控制器.h文件:
#import <UIKit/UIKit.h>
typedef void(^BToAblock)(NSString *string);
@interface B_ViewController : UIViewController
@property (nonatomic, copy)BToAblock block;
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@property (nonatomic, strong) UITextField *bTextField;
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.bTextField.layer.borderWidth = 1;
[self.view addSubview:self.bTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
_block(self.bTextField.text);
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
KVO传值
用法:反向传值:
需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController ()
@property (nonatomic, strong) UILabel *aLabel;
@property (nonatomic, strong) B_ViewController *bViewController;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aLabel.layer.borderColor = [UIColor grayColor].CGColor;
self.aLabel.layer.borderWidth = 1;
[self.view addSubview:self.aLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"push到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.bViewController = [[B_ViewController alloc] init];
[self.bViewController addObserver:self forKeyPath:@"string" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)buttonAction:(UIButton *)sender {
[self.navigationController pushViewController:self.bViewController animated:YES];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"string"]) {
self.aLabel.text = self.bViewController.string;
}
}
- (void)dealloc {
[self.bViewController removeObserver:self forKeyPath:@"string"];
}
@end
B控制器.h文件:
#import <UIKit/UIKit.h>
@interface B_ViewController : UIViewController
@property (nonatomic, copy) NSString *string;
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@property (nonatomic, strong) UITextField *bTextField;
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.bTextField.layer.borderWidth = 1;
[self.view addSubview:self.bTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
self.string = self.bTextField.text;
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
通知传值
用法:正向传值
需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。
用法:反向传值:
需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。
我们在此将两种传值情况写到一个Demo中,所以将上述Label换为Textfield即可,如下:
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController ()
@property (nonatomic, strong) UITextField *aTextField;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.aTextField.layer.borderWidth = 1;
[self.view addSubview:self.aTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"B2A" object:nil];
}
- (void)buttonAction:(UIButton *)sender {
B_ViewController *bViewController = [[B_ViewController alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"A2B" object:nil userInfo:@{@"key":self.aTextField.text}];
[self.navigationController pushViewController:bViewController animated:YES];
}
- (void)tzAction:(NSNotification *)sender {
self.aTextField.text = sender.userInfo[@"key"];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@property (nonatomic, strong) UITextField *bTextField;
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(instancetype)init
{
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"A2B" object:nil];
self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.bTextField.layer.borderWidth = 1;
[self.view addSubview:self.bTextField];
}
return self;
}
- (void)tzAction:(NSNotification *)sender {
self.bTextField.text = sender.userInfo[@"key"];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)buttonAction:(UIButton *)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"B2A" object:nil userInfo:@{@"key":self.bTextField.text}];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
单例传值
用法:正向传值
需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。
用法:反向传值:
需求:A-push到B,当B消失的时候,A中有一个Label需要显示从B中的一个TextField输入的内容,这时我们需要用到反向传值。
我们在此将两种传值情况写到一个Demo中,所以将上述Label换为Textfield即可,如下:
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
#import "DanLi.h"
@interface A_ViewController ()
@property (nonatomic, strong) UITextField *aTextField;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.aTextField.layer.borderWidth = 1;
[self.view addSubview:self.aTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)viewWillAppear:(BOOL)animated
{
DanLi *danli = [[DanLi alloc] init];
self.aTextField.text = danli.value;
}
- (void)buttonAction:(UIButton *)sender {
DanLi *danli = [DanLi sharedDanLi];
danli.value = self.aTextField.text;
B_ViewController *bViewController = [[B_ViewController alloc] init];
[self.navigationController pushViewController:bViewController animated:YES];
}
B控制器.m文件:
#import "B_ViewController.h"
#import "DanLi.h"
@interface B_ViewController ()
@property (nonatomic, strong) UITextField *bTextField;
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
self.bTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.bTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.bTextField.layer.borderWidth = 1;
[self.view addSubview:self.bTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.bTextField.text = [DanLi sharedDanLi].value;
}
- (void)buttonAction:(UIButton *)sender {
[DanLi sharedDanLi].value = self.bTextField.text;
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end
单例类.h文件:
#import <Foundation/Foundation.h>
@interface DanLi : NSObject
@property (atomic, copy) NSString *value;
+ (DanLi *)sharedDanLi;
@end
单例类.m文件:
#import "DanLi.h"
static DanLi *danli = nil;
@implementation DanLi
+ (DanLi *)sharedDanLi {
if (danli == nil) {
danli = [[DanLi alloc] init];
}
return danli;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
@synchronized(self) {
if (danli == nil) {
danli = [super allocWithZone:zone];
}
}
return danli;
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end
KVC传值
用法:正向传值
需求:当A-push到B时,B中有一个Label需要显示从A中的一个TextField输入的内容,这时我们需要用到正向传值。
A控制器.m文件:
#import "A_ViewController.h"
#import "B_ViewController.h"
@interface A_ViewController ()
@property (nonatomic, strong) UITextField *aTextField;
@end
@implementation A_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"A";
self.view.backgroundColor = [UIColor whiteColor];
self.aTextField = [[UITextField alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
self.aTextField.layer.borderColor = [UIColor grayColor].CGColor;
self.aTextField.layer.borderWidth = 1;
[self.view addSubview:self.aTextField];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传到B" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)sender {
B_ViewController *bViewController = [[B_ViewController alloc] init];
[bViewController setValue:self.aTextField.text forKey:@"string"];
[self.navigationController pushViewController:bViewController animated:YES];
}
@end
B控制器.h文件:
#import <UIKit/UIKit.h>
@interface B_ViewController : UIViewController
@property (nonatomic, copy) NSString *string;
@end
B控制器.m文件:
#import "B_ViewController.h"
@interface B_ViewController ()
@end
@implementation B_ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"B";
self.view.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
UILabel *bLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 200, 100, 30)];
bLabel.layer.borderColor = [UIColor grayColor].CGColor;
bLabel.layer.borderWidth = 1;
[self.view addSubview:bLabel];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2-50, 280, 100, 30);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitle:@"传值A" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
bLabel.text = self.string;
}
- (void)buttonAction:(UIButton *)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end