1. 代理传值
特点:一般用于反向传值,正向传值也可以,只不过不常用而已;
一对一
从 TwoVC 页面传值到 OneVC 页面,代码如下:
OneVC.m
@interface OneVC () <ReverseDelegate>
@end
@implementation OneVC
- (void)pushToTwoVC
{
TwoVC *twoVC = [[TwoVC alloc] init];
twoVC.reverseDelegate = self;
[self.navigationController pushViewController:twoVC animated:YES];
}
#pragma mark - 逆向传值的代理
- (void)reversePassValue:(NSString *)value
{
NSLog(@"反向传值为:%@", value);
}
@end
TwoVC.h
@protocol ReverseDelegate <NSObject>
- (void)reversePassValue:(NSString *)value;
@end
@interface TwoVC : UIViewController
@property (nonatomic, weak) id<ReverseDelegate> reverseDelegate;
@end
TwoVC.m
- (void)popToOneVC
{
[self.reverseDelegate reversePassValue:@"I love you too."];
[self.navigationController popViewControllerAnimated:YES];
}
2. Block 传值
特点:逆向传值
一对一
从 TwoVC 页面传值到 OneVC 页面,代码如下:
TwoVC.h
//定义你需要的block
typedef void (^ReverseBlock_name_age) (NSString *name, NSInteger age);
@interface TwoVC : UIViewController
/** 声明一个block属性,这个block是获取传值的界面传进来的,参数为 name,age */
@property (nonatomic, strong) ReverseBlock_name_age reverseBlock_name_age;
@end
TwoVC.m
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
__weak typeof(self) weakself = self;
weakself.name = @"panzhangbao";
weakself.age = 25;
if (_reverseBlock_name_age) {
_reverseBlock_name_age(weakself.name, weakself.age);
}
[weakself.navigationController popViewControllerAnimated:YES];
}
OneVC.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
TwoVC *twoVC = [[TwoVC alloc] init];
//在这里获取TwoVC传过来的值,并进行相应处理(如:打印)
twoVC.reverseBlock_name_age = ^(NSString *name, NSInteger age){
NSLog(@"反向Block传值为 name:%@, age:%ld", name, age);
};
[self.navigationController pushViewController:twoVC animated:YES];
}
3. 通知传值
特点:任意页面传值
一对多
发送通知页面
// 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"reversePassValueNOTE" object:@"reversePassValueNOTE" userInfo:@{@"key":@"I love you."}];
接收通知页面 - 可以为多个页面
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:nil object:@"reversePassValueNOTE"];
// 获取通知
- (void) getNotification:(NSNotification *)notificaion{
self.textField.text = notificaion.userInfo[@“key”];
}
// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"reversePassValueNOTE" object:nil];
4. 单例传值
特点:一定初始化,N 次调用
一对多
首先,新建一个单例类,代码如下:
Singleton.h
#import <Foundation/Foundation.h>
@interface Singleton : NSObject
@property (nonatomic, copy) NSString *passValue;
/** 创建实例方法 */
+ (instancetype)sharedInstance;
@end
Singleton.m
#import "Singleton.h"
@implementation Singleton
// 以下代码在单例类中的.m 文件中写
static id _singleton;
/** 一般只调用这个类方法就行,返回的是类对象。当然也可以在.h 文件中定义其他单例对象 */
+ (instancetype)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_singleton = [[self alloc] init];
});
return _singleton;
}
//重写allocWithZone:方法,在这里创建唯一的实例(注意线程安全)
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_singleton = [super allocWithZone:zone];
});
return _singleton;
}
- (id)copyWithZone:(NSZone *)zone
{
return _singleton;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
return _singleton;
}
@end
首页代码进行相应的初始化和赋值,代码如下:
ViewController.m
#import "ViewController.h"
#import "OtherVC.h"
#import "Singleton.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
Singleton *singleton = [Singleton sharedInstance];
singleton.passValue = @"你好,单例";
[self.navigationController pushViewController:[OtherVC new] animated:YES];
}
@end
在其他页面进行单例值的接收,代码如下:
Other.m
#import "OtherVC.h"
@interface OtherVC ()
@end
@implementation OtherVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Singleton *singleton = [Singleton sharedInstance];
NSLog(@" 获取的单例传值为:%@", singleton.passValue);
}
@end
网友评论