iOS 監(jiān)聽(tīng)回調(diào)機(jī)制KVO實(shí)例
監(jiān)聽(tīng)某個(gè)對(duì)象,如果這個(gè)對(duì)象的數(shù)據(jù)發(fā)生變化,會(huì)發(fā)送給監(jiān)聽(tīng)者從而觸發(fā)回調(diào)函數(shù)
[self.bean addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
這個(gè)就是注冊(cè)監(jiān)聽(tīng),這個(gè)@“data”作為標(biāo)識(shí)符方便回調(diào)函數(shù)辨認(rèn)
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"data"])
{
self.label.text = [self.bean valueForKey:@"data"];
}
}
這個(gè)就是回調(diào)函數(shù),分辨是哪個(gè)對(duì)象發(fā)生變化,然后給與相應(yīng)的處理
-(void)viewWillDisappear:(BOOL)animated{
[self.bean removeObserver:self forKeyPath:@"data"];
}
既然有注冊(cè)監(jiān)聽(tīng)還記得解除監(jiān)聽(tīng)
以下是完整例子代碼
//
// 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 監(jiān)聽(tīng)回調(diào)機(jī)制KVO實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IOS文件的簡(jiǎn)單讀寫(xiě)實(shí)例詳解
這篇文章主要介紹了IOS文件的簡(jiǎn)單讀寫(xiě)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Drawer?Builder組件實(shí)現(xiàn)flutter側(cè)邊抽屜效果示例分析
這篇文章主要為大家介紹了Drawer?Builder組件實(shí)現(xiàn)flutter側(cè)邊抽屜效果示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
iOS開(kāi)發(fā)學(xué)習(xí)之監(jiān)測(cè)程序的崩潰次數(shù)詳解
iOS開(kāi)發(fā)中遇到程序崩潰是很正常的事情,下面這篇文章主要給大家介紹了關(guān)于iOS如何監(jiān)測(cè)程序崩潰次數(shù)的相關(guān)資料,文中通過(guò)詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
iOS表情鍵盤(pán)的簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了iOS表情鍵盤(pán)的簡(jiǎn)單實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS 獲取已連接的wifi信息的實(shí)現(xiàn)代碼的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08

