KVO實例淺析
最近遇到個問題,在處理項目中一個評論界面時,因為直接用的是UIWebView展示評論列表,結果取到的頁面上下都有一段CGSize為(320,65)的亂七八糟的廣告,十分礙眼.頭部廣告因很方便的在頭部坐標貼上自己的logo解決了,但是尾部的,因為每個頁面的評論長短不一,坐標也就不一樣,這樣就不能給定死坐標去貼logo,思前想后,通過KVO很好的解決了這個問題.
@KVO概述:
KVO,即:Key-Value Observing,它提供一種機制,當指定的對象的屬性被修改后,則對象就會接受到通知。
簡單的說就是每次指定的被觀察的對象的屬性被修改后,KVO就會自動通知相應的觀察者了。
使用步驟如下:
1. 注冊,指定被觀察者的屬性,
2. 實現回調方法
3. 觸發回調方法
4. 移除觀察
代碼實例:
-(void)viewDidLoad{
// KVO,作為一個觀察者,只要屬性"contentSize"發生變化,回調方法里面就會通知
[_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
}
// 回調方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context
{
if(object == _webView.scrollView && [keyPath isEqualToString:@"contentSize"])
{
// 得到最大的Y坐標
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
{
// 調用父類的方法
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)dealloc{//---->在ARC環境下也能調用dealloc方法,只是不需要寫[super dealloc]
// 移除KVO,否則會引起資源泄露
[_webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
[super dealloc];
}
上面是針對contentSize屬性,其他屬性依此類推
KVC
通常,我們都是通過屬性的set和get方法來賦值和取值,這里介紹用Key-Value-Coding(KVC)鍵值編碼來給類的屬性賦值和取值.
1.基本方式(setValue:forKey: valueForKey)
// ---定義一個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個BOOL值變量,用來驗證KVC訪問屬性鍵順序
[student setValue:@(YES) forKey:@"test”];
結果是:_test—>_isTest—>test—>isTest
2.鍵路徑訪問(用于一個類中屬性的屬性 setValue:ForKeyPath: forKeyPath)
// 創建一個班級類
@interface HMTClass : NSObject
@property (nonatomic,copy)NSString * name;
@end
然后前面第一點中在Student類中寫了一個班級屬性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.自動封裝基本數據類型
我們在Student類中添加分數屬性 NSInteger number 學號;
#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*類型設置的屬性值@"100",而我們的屬性是NSInteger類型的,存取都沒有問題。
4.操作集合
在Student類中加入數組NSArray,用來表示其他的學生。
#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"]);
打印出來是數組(200,300,400)