iOS 監(jiān)聽回調(diào)機制KVO實例
監(jiān)聽某個對象,如果這個對象的數(shù)據(jù)發(fā)生變化,會發(fā)送給監(jiān)聽者從而觸發(fā)回調(diào)函數(shù)
[self.bean addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
這個就是注冊監(jiān)聽,這個@“data”作為標識符方便回調(diào)函數(shù)辨認
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"data"])
{
self.label.text = [self.bean valueForKey:@"data"];
}
}
這個就是回調(diào)函數(shù),分辨是哪個對象發(fā)生變化,然后給與相應(yīng)的處理
-(void)viewWillDisappear:(BOOL)animated{
[self.bean removeObserver:self forKeyPath:@"data"];
}
既然有注冊監(jiān)聽還記得解除監(jiān)聽
以下是完整例子代碼
//
// 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)聽回調(diào)機制KVO實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
IOS使用NSUserDefault去實現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲
這篇文章主要介紹了IOS使用NSUserDefault去實現(xiàn)界面?zhèn)髦岛蛿?shù)據(jù)存儲的相關(guān)資料,需要的朋友可以參考下2017-07-07
iOS中tableView cell分割線的一些設(shè)置技巧
在項目開發(fā)中我們會常常遇到tableView 的cell分割線顯示不全,左邊會空出一截像素,更有甚者想改變系統(tǒng)的分割線,下面通過這篇文章來一起學(xué)習(xí)學(xué)習(xí)在iOS中tableView cell分割線的一些設(shè)置技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05

