一下所有的演示由:AViewController 和 BViewController 完成简写(AVC和BVC)
所有传值合集github链接:https://github.com/TianYou899/valueset
1.属性传值(AVC代码传到BVC代码):
AVC 导入 BVC
BVC声明属性
//接收值
@property (nonatomic, copy) NSString * string;
BViewController * bVC = [[BViewController alloc]init];
bVC.string = self.aTextField.text;
[self.navigationController pushViewController:bVC animated:YES];
2.代理传值(BVC传到AVC):
BVC:
<span style="font-size:12px;">//定义代理
@protocol delegateCZ <NSObject>
// 代理方法
- (void)transferString:(NSString *)string;
@end
@interface BViewController : UIViewController
// 代理属性
@property (nonatomic, weak) id<delegateCZ> delegate;</span>
<span style="font-size:12px;"></span><pre name="code" class="objc"> // 判断有没用遵循代理 有就执行
if (self.delegate && [self.delegate conformsToProtocol:@protocol(delegateCZ)]) {
[self.delegate transferString:self.bTextField.text];
}
[self.navigationController popToRootViewControllerAnimated:YES];
AVC
@interface AViewController ()<delegateCZ>
BViewController * bVC = [[BViewController alloc]init];
// 遵循代理
bVC.delegate = self;
// 从 B 从到A
// 执行代理方法
- (void)transferString:(NSString *)string {
self.aTextField.text = string;
}
3.Block传值(BVC传到AVC):
BVC
// 定义一个block
typedef void (^blockCZ)(NSString * string);
@interface BViewController : UIViewController
// block 属性
@property (nonatomic,copy) blockCZ block;
/**
Blcok 传值 B传到A
注意:因为A页面堆过来的时候已经初始化了B 所有不能从A传到B(这种情况下硬传的话 会造成循环引用)
*/
_block(self.bTextField.text);
[self.navigationController popToRootViewControllerAnimated:YES];
AVC
BViewController * bVC = [[BViewController alloc]init];
[bVC setBlock:^(NSString * string){
self.aTextField.text = string;
}];
[self.navigationController pushViewController:bVC animated:YES];
4.单例传值(AVC传到BVC)
声明单例类.h
@interface DanLi : NSObject
@property (atomic, copy) NSString *value;
+ (DanLi *)sharedDanLi;
@end
声明单例类.m
<pre name="code" class="objc">#import "DanLi.h"
static DanLi *danli = nil;
@implementation DanLi
//实现方法,判断是否为空,是就创建一个全局实例给它
+ (DanLi *)sharedDanLi {
if (danli == nil) {
danli = [[DanLi alloc] init];
}
return danli;
}
//避免alloc/new创建新的实例变量
+ (id)allocWithZone:(struct _NSZone *)zone {
@synchronized(self) {
if (danli == nil) {
danli = [super allocWithZone:zone];
}
}
return danli;
}
//避免copy,需要实现NSCopying协议
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end
5.通知传值(BVC传到AVC)
AVC
// 接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tzAction:) name:@"tz" object:nil];
// 回调通知
- (void)tzAction:(NSNotification *)sender {
self.aTextField.text = sender.userInfo[@"key"];
}
BVC
[[NSNotificationCenter defaultCenter] postNotificationName:@"tz" object:nil userInfo:@{@"key":self.bTextField.text}];
[self.navigationController popToRootViewControllerAnimated:YES];
6.KVC传值(AVC传到BVC)
AVC
BViewController * bVC = [[BViewController alloc]init];
//KVC 传值:这里只能传A传到B (因为 B在A页面提前初始化)
//这里forkey 一定要和B 属性名字一致 (也可以用@"_string")因为是属性
// 给B属性string 赋值
[bVC setValue:self.aTextField.text forKey:@"string"];
[self.navigationController pushViewController:bVC animated:YES];
BVC
//接收值
@property (nonatomic, copy) NSString * string;
// KVC 接收值
self.bTextField.text = self.string;
所有传值合集github链接:https://github.com/TianYou899/valueset