欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

iOS 监听回调机制KVO实例

时间:2021-04-15 11:17:18|栏目:iOS代码|点击:

监听某个对象,如果这个对象的数据发生变化,会发送给监听者从而触发回调函数

[self.bean addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

这个就是注册监听,这个@“data”作为标识符方便回调函数辨认

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
 if([keyPath isEqualToString:@"data"])
 {
  self.label.text = [self.bean valueForKey:@"data"];
 }
}

这个就是回调函数,分辨是哪个对象发生变化,然后给与相应的处理

-(void)viewWillDisappear:(BOOL)animated{
 [self.bean removeObserver:self forKeyPath:@"data"];
}

既然有注册监听还记得解除监听

以下是完整例子代码

//
// ViewController.m
// First
//
// Created by shanreal-iOS on 17/10/16.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "ViewController.h"
#import "TestBean.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel* label;
@property(nonatomic,strong)UIButton* btn;
@property(nonatomic,strong)TestBean* bean;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 // Do any additional setup after loading the view.

 self.bean = [[TestBean alloc]init];
 [self.bean setValue:@"1" forKey:@"data"];
 
 self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, 100, 30)];
 self.label.textColor = [UIColor blackColor];
 self.label.text = [self.bean valueForKey:@"data"];
 [self.view addSubview:self.label];
 
 
 self.btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 200, 30)];
 [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 [self.btn setTitle:@"chanage data" forState:UIControlStateNormal];
 [self.btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:self.btn];
 
}
-(void)viewWillAppear:(BOOL)animated{
 [self.bean addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
 
}
-(void)clickAction{
 int data = [[self.bean valueForKey:@"data"] intValue]+1;
 self.bean.data = [NSString stringWithFormat:@"%d",data];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
 if([keyPath isEqualToString:@"data"])
 {
  self.label.text = [self.bean valueForKey:@"data"];
 }
}
-(void)viewWillDisappear:(BOOL)animated{
 [self.bean removeObserver:self forKeyPath:@"data"];
}

@end

#import <Foundation/Foundation.h>
@interface TestBean : NSObject{
 NSString* data;
}
@property(nonatomic,assign)int id;
@property(nonatomic,strong)NSString* data;
@end

#import "TestBean.h"
@implementation TestBean
@end

上一篇:iOS 控件封装(又名拧螺丝)之排序按钮的开发

栏    目:iOS代码

下一篇:iOS开发之UIScrollView详解

本文标题:iOS 监听回调机制KVO实例

本文地址:http://www.codeinn.net/misctech/101617.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有