KVO實(shí)例淺析
最近遇到個(gè)問題,在處理項(xiàng)目中一個(gè)評(píng)論界面時(shí),因?yàn)橹苯佑玫氖荱IWebView展示評(píng)論列表,結(jié)果取到的頁面上下都有一段CGSize為(320,65)的亂七八糟的廣告,十分礙眼.頭部廣告因很方便的在頭部坐標(biāo)貼上自己的logo解決了,但是尾部的,因?yàn)槊總€(gè)頁面的評(píng)論長短不一,坐標(biāo)也就不一樣,這樣就不能給定死坐標(biāo)去貼logo,思前想后,通過KVO很好的解決了這個(gè)問題.
@KVO概述:
KVO,即:Key-Value Observing,它提供一種機(jī)制,當(dāng)指定的對(duì)象的屬性被修改后,則對(duì)象就會(huì)接受到通知。
簡單的說就是每次指定的被觀察的對(duì)象的屬性被修改后,KVO就會(huì)自動(dòng)通知相應(yīng)的觀察者了。
使用步驟如下:
1. 注冊,指定被觀察者的屬性,
2. 實(shí)現(xiàn)回調(diào)方法
3. 觸發(fā)回調(diào)方法
4. 移除觀察
代碼實(shí)例:
-(void)viewDidLoad{
// KVO,作為一個(gè)觀察者,只要屬性"contentSize"發(fā)生變化,回調(diào)方法里面就會(huì)通知
[_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
}
// 回調(diào)方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context
{
if(object == _webView.scrollView && [keyPath isEqualToString:@"contentSize"])
{
// 得到最大的Y坐標(biāo)
CGSize size = _webView.scrollView.contentSize;
if (size.height > 568.0) {
// 遮擋廣告
_hideBottomImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, size.height-67, ScreenWidth, 67)];
_hideBottomImage.image = [UIImage imageNamed:@"banner"];
[_webView.scrollView addSubview:_hideBottomImage];
[_hideBottomImage release];
}
}
else
{
// 調(diào)用父類的方法
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc{//---->在ARC環(huán)境下也能調(diào)用dealloc方法,只是不需要寫[super dealloc]
// 移除KVO,否則會(huì)引起資源泄露
[_webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
[super dealloc];
}
上面是針對(duì)contentSize屬性,其他屬性依此類推
KVC
通常,我們都是通過屬性的set和get方法來賦值和取值,這里介紹用Key-Value-Coding(KVC)鍵值編碼來給類的屬性賦值和取值.
1.基本方式(setValue:forKey: valueForKey)
// ---定義一個(gè)Student類(.m文件無任何操作)
#import <Foundation/Foundation.h>
@class HMTClass;
@interface HMTStudent : NSObject{
NSString * _name;
BOOL _test;
BOOL _isTest;
BOOL test;
BOOL isTest;
}
@property (nonatomic,copy)NSString * name;
@property (nonatomic,copy)NSString * sex;
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,strong) HMTClass * hmtClass;
@end
// ---main文件
HMTStudent * student = [[HMTStudent alloc] init];
student.hmtClass = [[HMTClass alloc] init];
student.name = @"humingtao”; // set方法賦值
// KVC賦值
[student setValue:@“mawei is dog" forKey:@"name”];
[student setValue:@"m" forKey:@"sex"];
[student setValue:@(10) forKey:@"age"];
// 取值
NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,[student valueForKey:@"name"]);
特別注意:
我在類里面還定義了4個(gè)BOOL值變量,用來驗(yàn)證KVC訪問屬性鍵順序
[student setValue:@(YES) forKey:@"test”];
結(jié)果是:_test—>_isTest—>test—>isTest
2.鍵路徑訪問(用于一個(gè)類中屬性的屬性 setValue:ForKeyPath: forKeyPath)
// 創(chuàng)建一個(gè)班級(jí)類
@interface HMTClass : NSObject
@property (nonatomic,copy)NSString * name;
@end
然后前面第一點(diǎn)中在Student類中寫了一個(gè)班級(jí)屬性hmtClass
HMTClass *hmtClass = [[HMTClass alloc]init];
[hmtClass setValue:@"宇宙一班" forKey:@"name"];
[student setValue:hmtClass forKey:@"hmtClass"];
NSString *hmtClassName = [student valueForKeyPath:@"hmtClass.name"];
//也可以這樣存值
[student setValue:@"宇宙一班" forKeyPath:@"hmtClass.name"];
student.hmtClass.name = [student valueForKeyPath:@"hmtClass.name"];
3.自動(dòng)封裝基本數(shù)據(jù)類型
我們在Student類中添加分?jǐn)?shù)屬性 NSInteger number 學(xué)號(hào);
#import <Foundation/Foundation.h>
@class HMTClass;
@interface HMTStudent : NSObject
{
NSString *_name;
NSInteger number;
}
@end
[student setValue:@"100" forKeyPath:@"number"];
NSString *number = [student valueForKey:@"number"];
可見用NSString*類型設(shè)置的屬性值@"100",而我們的屬性是NSInteger類型的,存取都沒有問題。
4.操作集合
在Student類中加入數(shù)組NSArray,用來表示其他的學(xué)生。
#import <Foundation/Foundation.h>
@class HMTClass;
@interface HMTStudent : NSObject
{
NSArray *manyStudents;
}
@end
Student *student1 = [[HMTStudent alloc]init];
Student *student2 = [[HMTStudent alloc]init];
Student *student3 = [[HMTStudent alloc]init];
[student1 setValue:@"200" forKey:@"number"];
[student2 setValue:@"300" forKey:@"number"];
[student3 setValue:@"400" forKey:@"number"];
NSArray *array = [NSArray arrayWithObjects:student1,student2,student3,nil];
[student setValue:array forKey:@"manyStudents"];
NSLog(@"%@",[student valueForKeyPath:@"manyStudents.number"]);
打印出來是數(shù)組(200,300,400)