KVO传值
什么是KVO?
KVO全称Key Value Observing,是苹果提供的一套事件通知机制。允许对象监听另一个对象特定属性的改变,并在改变时接收到事件。由于KVO的实现机制,只针对属性才会发生作用,一般继承自NSObject的对象都默认支持KVO。
KVO可以监听单个属性的变化,也可以监听集合对象的变化。通过KVC的mutableArrayValueForKey:等方法获得代理对象,当代理对象的内部对象发生改变时,会回调KVO监听的方法。集合对象包含NSArray和NSSet。
实现KVO传值:
- 注册观察者
- 在观察者中实现实现observeValueForKeyPath:ofObject:change:context:方法
示例:
- 创建一个继承于NSObject类的TextObj
#import <Foundation/Foundation.h>
@interface TextObj : NSObject
@property (assign, nonatomic) int numInTextField;
@end
- 在viewController中:
#import "ViewController.h"
#import "TextObj.h"
@interface ViewController ()
@property (strong, nonatomic) UITextField* contentField;
@property (strong, nonatomic) TextObj* textObj;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor systemGray6Color];
UIButton* button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setTitle:@"changeText" forState:UIControlStateNormal];
button.frame = CGRectMake(200, 300, 80, 40);
[button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_contentField = [[UITextField alloc]initWithFrame:CGRectMake(80, 200, [UIScreen mainScreen].bounds.size.width - 160, 40)];
[self.view addSubview:_contentField];
_contentField.text = @"这里是原文字";
_textObj = [[TextObj alloc]init];
[_textObj addObserver:self forKeyPath:@"numInTextField" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
_textObj.numInTextField = 0;
}
- (void) pressButton {
//监视对象在这被改变,会引起KVO调用一个方法
_textObj.numInTextField++;
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
NSString* str = [[NSString alloc]initWithFormat:@"被改变的第%d次",_textObj.numInTextField ];
_contentField.text = str;
}
- (void) dealloc {
[_textObj removeObserver:self forKeyPath:@"numInTextField"];
}
@end
点击button,实现语句的改变
Block传值
block是什么
block的定义:带有自动变量(局部变量)的匿名函数
可以通过一篇博客初步理解一下
关于block
实现block传值:
- 声明block(ViewControllerSecond.h)
- 调用block(ViewControllerSecond.m)
- 实现block(ViewController.m)
1:
typedef void(^returnValue)(NSString* str);
@interface ViewControllerSecond : UIViewController
@property (strong, nonatomic) returnValue blockValue;
@end
2:
_blockValue(_textField.text);
3:
secondController.blockValue = ^(NSString* str) {
self.label.text = str;
};
实例:
ViewControllerSecond.h
#import <UIKit/UIKit.h>
typedef void(^returnValue)(NSString* str);
@interface ViewControllerSecond : UIViewController
@property (strong, nonatomic) returnValue blockValue;
@property (strong, nonatomic) UITextField* textField;
@end
ViewControllerSecond.m
// ViewControllerSecond.m
#import "ViewControllerSecond.h"
@interface ViewControllerSecond ()
@end
@implementation ViewControllerSecond
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor systemGray5Color];
_textField = [[UITextField alloc]init];
_textField.layer.borderColor = [[UIColor purpleColor] CGColor];
_textField.layer.borderWidth = 1.0;
_textField.frame = CGRectMake(100, 100, 100, 40);
[self.view addSubview:_textField];
UIButton* backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton addTarget:self action:@selector(returnFirstView) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
backButton.frame = CGRectMake(200, 200, 60, 40);
[self.view addSubview:backButton];
}
- (void) returnFirstView {
_blockValue(_textField.text);
[self dismissViewControllerAnimated:NO completion:nil];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) UILabel* label;
@end
ViewController.m
#import "ViewController.h"
#import "ViewControllerSecond.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(200, 300, 60, 40);
[self.view addSubview:button];
[button setTitle:@"inside" forState:UIControlStateNormal];
[button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
_label = [[UILabel alloc]init];
_label.text = @"现在label内容未被修改";
_label.frame = CGRectMake(100, 100, [UIScreen mainScreen].bounds.size.width - 200, 50);
[self.view addSubview:_label];
}
- (void) pressButton {
ViewControllerSecond* secondController = [[ViewControllerSecond alloc]init];
secondController.modalPresentationStyle = UIModalPresentationFullScreen;
secondController.blockValue = ^(NSString* str) {
self.label.text = str;
};
[self presentViewController:secondController animated:YES completion:nil];
}
@end
ViewController的界面:
点击button,进入ViewControllerSecond界面:
在TextField里面输入了“888”,点击按钮返回
传值有效。