一. 通知传值
通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面。
通知传值可以分为三个步骤:
1.在发送者中实现一个方法进行发送通知
- (void) pressadd {
self.dictory = [NSMutableDictionary dictionaryWithObjectsAndKeys:self.labcity, @"city",self.labtemp, @"temp", self.labweather, @"text", self.hourarray, @"hourarr" ,self.temparray, @"temparr", self.weatherarray, @"weatherarr",self.weatharray,@"weatharr",self.dayarray, @"dayarr", self.tempMaxarray,@"tempmaxarr", self.tempMinarray, @"tempminarr", nil];
//实现一个方法进行发送通知这里以发送字典为例
[[NSNotificationCenter defaultCenter] postNotificationName:@"TransDataNoti" object:nil userInfo:_dictory];
[self dismissViewControllerAnimated:YES completion:nil];
}
2.在接收者中注册通知,也就是接收者要进行接收通知,接收通知和发送通知的名字要一致
//接收通知和发送通知的名字要一致(名字都为TransDataNoti)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiReceived:) name:@"TransDataNoti" object:nil];
3.在接收者中实现通知中的方法
- (void) notiReceived: (NSNotification*) sender {
[self.arrcity addObject:sender.userInfo[@"city"]];
[self.arrnowtemp addObject:sender.userInfo[@"temp"]];
[self.arrnowweather addObject:sender.userInfo[@"text"]];
[self.hourarray addObject:sender.userInfo[@"hourarr"]];
[self.temparray addObject:sender.userInfo[@"temparr"]];
[self.weatherarray addObject:sender.userInfo[@"weatherarr"]];
[self.weatharray addObject:sender.userInfo[@"weatharr"]];
[self.dayarray addObject:sender.userInfo[@"dayarr"]];
[self.tempMaxarray addObject:sender.userInfo[@"tempmaxarr"]];
[self.tempMinarray addObject:sender.userInfo[@"tempminarr"]];
[_tableview reloadData];
}
4.在不用的时候移除通知
- (void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"TransDataNoti" object:nil];
}
注意参数Observer为要删除的观察者,一定不能置为nil。
如果为nil它会报警告:
Null passed to a callee that requires a non-null argument。
iOS9以上对其不进行移除处理不会崩溃,但是如果这个对象是在监听一个单例对象的属性的话,在其他地方,又修改该属性,还是会崩溃。
二. KOV传值
KVO全称Key Value Observing,是苹果提供的一套事件通知机制。允许对象监听另一个对象特定属性的改变,并在改变时接收到事件。由于KVO的实现机制,只针对属性才会发生作用,一般继承自NSObject的对象都默认支持KVO。
KVO可以监听单个属性的变化,也可以监听集合对象的变化。通过KVC的mutableArrayValueForKey:等方法获得代理对象,当代理对象的内部对象发生改变时,会回调KVO监听的方法。集合对象包含NSArray和NSSet。
1.KVO的基本使用方法
KVO的使用大致分为三步:
一.首先通过addObserver:forKeyPath:options:context:来注册观察者,观察可以接受KeyPath属性的变化事件。
/*
@observer:就是观察者,是谁想要观测对象的值的改变。
@keyPath:就是想要观察的对象属性。
@options:options一般选择NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld,这样当属性值发生改变时我们可以同时获得旧值和新值,如果我们只填NSKeyValueObservingOptionNew则属性发生改变时只会获得新值。
@context:想要携带的其他信息,比如一个字符串或者字典什么的,不过只能是同一视图的内容。
*/
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
@options分为四个类型:
- NSKeyValueObservingOptionNew 返回改变后的新值。
- NSKeyValueObservingOptionOld 返回改变之前的旧值。
- NSKeyValueObservingOptionInitial 注册的时候就会发一次通知,改变后也会发通知。
- NSKeyValueObservingOptionPrior 改变之前发一次通知,改变之后发一次通知。
二.在观察者中实现observeValueForKeyPath:ofObject:change:context:方法,当keyPath属性发生改变后,KVO会回调这个方法来通知观察者。
监听方法(回调方法):
/*
@keyPath:观察的属性
@object:观察的是哪个对象的属性
@change:这是一个字典类型的值,通过键值对显示新的属性值和旧的属性值
@context:上面添加观察者时携带的信息
*/
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context;
三.当观察者不需要监听时,可以调用removeObserver:forKeyPath:方法将KVO移除。
移除监听:
/*
@observer:就是观察者,是谁想要观测对象的值的改变。
@keyPath:就是想要观察的对象属性。
*/
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath;
单界面小实例:
- 先新建一个NSObject的子类Dog
- Dog.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Dog : NSObject
@property (nonatomic, assign) int age;
@end
NS_ASSUME_NONNULL_END
- ViewController.h
#import <UIKit/UIKit.h>
#import "Dog.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) UILabel* label;
@property (nonatomic, strong) UIButton* button;
@property (nonatomic, strong) Dog* dog;
@end
- ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.label = [[UILabel alloc] init];
self.label.frame = CGRectMake(100, 300, 200, 50);
[self.view addSubview:self.label];
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(150, 400, 60, 40);
[self.button setTitle:@"add" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(add:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
/*
@observer:就是观察者,是谁想要观测对象的值的改变。
@keyPath:就是想要观察的对象属性。
@options:options一般选择NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld,这样当属性值发生改变时我们可以同时获得旧值和新值,如果我们只填NSKeyValueObservingOptionNew则属性发生改变时只会获得新值。
@context:想要携带的其他信息,比如一个字符串或者字典什么的,不过只能是同一视图的内容。
*/
//初始化Dog
self.dog = [[Dog alloc] init];
//注册self.dog的监听者,把dog的属性age作为监听对象。
[self.dog addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
//实现UIButton的事件,来触发监听
- (void) add: (UIButton*) button {
static int a = 0;
//改变self.dog中的age的值,会调用监听方法。
self.dog.age = a++;
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSString* string = [[NSString alloc] initWithFormat:@"狗的年龄为:%d", self.dog.age];
self.label.text =string;
NSLog(@"old age: %@ --- new age: %@",[change valueForKey:@"old"], [change valueForKey:@"new"]);
}
- (void) dealloc {
[self.dog removeObserver:self forKeyPath:@"age"];
}
@end
- 效果图
三. Block传值
Block简介
将函数及其执行上下文封装起来的对象,block的调用实际就是函数的调用。
Block传值的步骤
- 步骤1:block 的声明 (视图二 .h 中)
//自定义类型Block
typedef void(^returnLabelValue) (NSString* label);
//声明一个returnLabelValue属性,这个Block是获取传值的界面传进来的
@property (nonatomic, strong) returnLabelValue valueLabel;
- 步骤2:block 的调用 (视图二 .m 中)
self.valueLabel(self.textField.text);
- 步骤3:block 实现(视图一新视图的创建中)
change.valueLabel = ^(NSString * text) {
self.label.text = text;
};
Block传值的测试
- FirstViewCollecter.h(第二个视图)
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
//自定义类型Block
typedef void(^returnLabelValue) (NSString* label);
@interface FirstViewController : UIViewController <UITextFieldDelegate>
@property (nonatomic, strong) UIButton* backButton;
@property (nonatomic, strong) UITextField* textField;
//声明一个returnLabelValue属性,这个Block是获取传值的界面传进来的
@property (nonatomic, strong) returnLabelValue valueLabel;
@end
NS_ASSUME_NONNULL_END
- FirstViewCollecter.m(第二个视图)
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.orangeColor;
self.backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.backButton.frame = CGRectMake(200, 200, 60, 40);
[self.backButton setTitle:@"返回" forState:UIControlStateNormal];
[self.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.backButton];
self.textField = [[UITextField alloc] init];
self.textField.frame = CGRectMake(100, 300, 250, 50);
self.textField.delegate = self;
self.textField.keyboardType = UIKeyboardTypeDefault;
self.textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:self.textField];
}
- (void) back {
if (self.valueLabel) {
self.valueLabel(self.textField.text);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
- ViewCollecter.m(第一个视图)
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) UILabel* label;
@property (nonatomic, strong) UIButton* button;
@end
- ViewCollecter.m(第一个视图)
#import "ViewController.h"
#import "FirstViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.label = [[UILabel alloc] init];
self.label.frame = CGRectMake(100, 100, 100, 50);
self.label.text = @"内容";
[self.view addSubview:self.label];
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.button setTitle:@"跳转" forState:UIControlStateNormal];
self.button.frame = CGRectMake(200, 300, 60, 40);
[self.button addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void) change {
FirstViewController* change = [[FirstViewController alloc] init];
change.modalPresentationStyle = UIModalPresentationFullScreen;
change.valueLabel = ^(NSString * text) {
self.label.text = text;
};
[self presentViewController:change animated:YES completion:nil];
}
@end
- 效果图
第一个视图:
第二个界面:
传值后的第一个界面:
四. 代理传值
协议传值是一种反向传值,在界面传值的过程中,是从后一个界面向前一个界面传值。例如,想要实现登录注册的话,就得把注册界面注册的账号和密码,传到登陆界面中,然后在登陆界面中进行之后的一系列操作。
下面把第一个界面用a来代替,第二个界面用b来代替。
实现协议传值(从b界面到a界面)
1. 在b界面中创建一个协议,以及协议所包含的方法
@protocol VB < NSObjec t>
- (void) changeColor: (UIColor*) color;
@end
@interface VB: ViewController
@property (nonatomic, assign) id <VB> delegate;
@end
2. 让a界面中实现遵守该协议,并且实现协议中的方法
@interface VA: ViewController <VB>
- (void) changeColor: (UIColor*) color;
@end
@implementation VA
- (void) changerColor: (UIColor*) color {
self.view.backgroundcolor = color;
}
3. 在a界面中让b界面显示出来
//推出视图控制器2
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
VB *vc2 = [[VB alloc] init];
//将当前的控制器做为代理对象赋值
vc2.delegate = self;
[self.navigationController pushViewController:vc2 animated:YES];
}
4. 在b界面中传值
- (void)press {
[_delegate changColor:redColor];
}
五. 属性传值
属性传值和协议传值最重要的一点不同,属性传值是在从a界面切换到b界面的时候直接将值传过去,对b界面的属性进行赋值,此时通过a界面所切换过来的b界面的属性就有其该有的值。
1. 在b界面中设置相应的属性
@interface SecondViewController : ViewController
@property (nonatomic, strong) UIColor *newcolor;
@end
2. 在a界面中对b界面的属性赋值
//推出视图控制器2
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
VB *vc2 = [[VB alloc] init];
vc2.newcolor = [UIColor orangeColor];
[self.navigationController pushViewController:vc2 animated:YES];
}
3. b界面中的属性就有值了
self.view.backgroundColor = _newcolor;