最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

以實例講解Objective-C中的KVO與KVC機制

 更新時間:2015年09月12日 10:33:21   作者:TommyYaphetS  
這篇文章主要介紹了以實例講解Objective-C中的KVO與KVC機制,即Key-Value-Observing與Key-Value-Coding,需要的朋友可以參考下

KVO實例淺析

最近遇到個問題,在處理項目中一個評論界面時,因為直接用的是UIWebView展示評論列表,結(jié)果取到的頁面上下都有一段CGSize為(320,65)的亂七八糟的廣告,十分礙眼.頭部廣告因很方便的在頭部坐標(biāo)貼上自己的logo解決了,但是尾部的,因為每個頁面的評論長短不一,坐標(biāo)也就不一樣,這樣就不能給定死坐標(biāo)去貼logo,思前想后,通過KVO很好的解決了這個問題.
@KVO概述:
KVO,即:Key-Value Observing,它提供一種機制,當(dāng)指定的對象的屬性被修改后,則對象就會接受到通知。
簡單的說就是每次指定的被觀察的對象的屬性被修改后,KVO就會自動通知相應(yīng)的觀察者了。

使用步驟如下:
1. 注冊,指定被觀察者的屬性,
2. 實現(xiàn)回調(diào)方法
3. 觸發(fā)回調(diào)方法  
4. 移除觀察

代碼實例:

復(fù)制代碼 代碼如下:

-(void)viewDidLoad{ 
 
    // KVO,作為一個觀察者,只要屬性"contentSize"發(fā)生變化,回調(diào)方法里面就會通知 
    [_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,否則會引起資源泄露  
    [_webView.scrollView removeObserver:self forKeyPath:@"contentSize"]; 
    [super dealloc];   
 

上面是針對contentSize屬性,其他屬性依此類推

KVC
通常,我們都是通過屬性的set和get方法來賦值和取值,這里介紹用Key-Value-Coding(KVC)鍵值編碼來給類的屬性賦值和取值.
1.基本方式(setValue:forKey:      valueForKey)

復(fù)制代碼 代碼如下:

//  ---定義一個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”]; 
 
       結(jié)果是:_test—>_isTest—>test—>isTest 

2.鍵路徑訪問(用于一個類中屬性的屬性 setValue:ForKeyPath: forKeyPath)

復(fù)制代碼 代碼如下:

//  創(chuàng)建一個班級類 
@interface HMTClass : NSObject 
 
@property (nonatomic,copy)NSString * name; 
 
@end 
 
然后前面第一點中在Student類中寫了一個班級屬性hmtClass 
復(fù)制代碼 代碼如下:
 
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.自動封裝基本數(shù)據(jù)類型
我們在Student類中添加分?jǐn)?shù)屬性 NSInteger number 學(xué)號;
復(fù)制代碼 代碼如下:

#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é)生。

復(fù)制代碼 代碼如下:

#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) 

相關(guān)文章

最新評論

莱西市| 阳江市| 克拉玛依市| 商城县| 安溪县| 涿鹿县| 海城市| 固阳县| 日照市| 萨嘎县| 深水埗区| 宜阳县| 大邑县| 阳江市| 陇西县| 政和县| 宁安市| 舟山市| 珲春市| 沭阳县| 措勤县| 芦溪县| 临朐县| 苏尼特左旗| 平度市| 宁国市| 息烽县| 屯门区| 南阳市| 鄂托克前旗| 四川省| 南靖县| 华蓥市| 壤塘县| 牟定县| 万年县| 墨竹工卡县| 舞钢市| 合肥市| 盘山县| 凤阳县|