【iOS】KVO传值和Block传值

KVO传值

什么是KVO?

KVO全称Key Value Observing,是苹果提供的一套事件通知机制。允许对象监听另一个对象特定属性的改变,并在改变时接收到事件。由于KVO的实现机制,只针对属性才会发生作用,一般继承自NSObject的对象都默认支持KVO。

KVO可以监听单个属性的变化,也可以监听集合对象的变化。通过KVC的mutableArrayValueForKey:等方法获得代理对象,当代理对象的内部对象发生改变时,会回调KVO监听的方法。集合对象包含NSArray和NSSet。

实现KVO传值:

  1. 注册观察者
  2. 在观察者中实现实现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传值:

  1. 声明block(ViewControllerSecond.h)
  2. 调用block(ViewControllerSecond.m)
  3. 实现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”,点击按钮返回

在这里插入图片描述
传值有效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值