美文网首页
三方库KVOController的使用及源码分析

三方库KVOController的使用及源码分析

作者: dandelionYD | 来源:发表于2019-01-08 12:52 被阅读0次

    KVOController的github地址

    pod 'KVOController'
    
    @interface TestModel : NSObject
    @property (nonatomic,strong)NSString  *name;
    @property (nonatomic,assign)int  age;
    @end
    
    @implementation TestModel
    @end
    
    ------------------------------------------------
    
    #import "KVOBaseUsesViewController_14.h"
    #import "KVOBaseUsesViewController_14_2.h"
    #import <KVOController/KVOController.h>
    
    @interface KVOBaseUsesViewController_14 ()
    @property (nonatomic,strong)TestModel  *t;
    @property (nonatomic,strong)FBKVOController *fbKVOController;
    @end
    
    @implementation KVOBaseUsesViewController_14
    - (void)viewDidLoad {
       [super viewDidLoad];
      
       //初始化:方式1 (类方法) 
       self.fbKVOController = [FBKVOController controllerWithObserver:self];
      
       //初始化:方式2 (实例方法)
       self.fbKVOController = [[FBKVOController   alloc]initWithObserver:self];
       
       //初始化:方式3 (实例方法) 持有 默认YES
       self.fbKVOController = [[FBKVOController   alloc]initWithObserver:self retainObserved:YES];
        
       //初始化:方式4 (实例方法) 不持有
       self.fbKVOController = [[FBKVOController   alloc]initWithObserver:self retainObserved:NO];
       
       -------------------------------------------------------------
       
       //监听:方式1 通过SEL来监听
       [self.fbKVOController observe:self.t keyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld action:@selector(propertyHasChanged:)];
       
       //监听:方式2 通过block来监听
       [self.fbKVOController observe:self.t keyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
              NSLog(@"监听者%@监听到%@的属性值改变了 - %@",observer, object, change);
         }];
        
        
       //监听:方式3 带上下文 实现:observeValueForKeyPath方法来监听
       [self.fbKVOController observe:self.t keyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"theWay3"];
        
        //监听:方式4  可以监听多个属性 通过SEL来监听
        [self.fbKVOController observe:self.t keyPaths:@[@"name",@"age"] options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld action:@selector(propertyHasChanged:)];
    
        
        //监听:方式5  可以监听多个属性 通过block监听
        [self.fbKVOController observe:self.t keyPaths:@[@"name",@"age"] options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
            NSLog(@"监听者%@监听到%@的属性值改变了 - %@",observer, object, change);
       }];
        
        //监听:方式6  可以监听多个属性 带上下文 实现:observeValueForKeyPath方法来监听
       [self.fbKVOController observe:self.t keyPaths:@[@"name",@"age"] options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:@"theWay6"];
    }
    
    
    //通过上下文的方法来监听的(实现监听方法)
    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
         NSLog(@"监听到%@的%@属性值改变了 - %@ - %@", object, keyPath, change, context);
    }
    
    //通过SEL的方法来监听的(实现监听方法)
    -(void)propertyHasChanged:(id)sender{
        NSLog(@"属性改变啦:%@",sender);
    }
    
    
    //点击修改属性
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        self.t.name = @"Lucy";
        self.t.age = 18;
    }
    
    -(void)dealloc{
        //注销方式1
        [self.fbKVOController unobserve:self.t keyPath:@"name"];   
        //[self.fbKVOController unobserve:self.t keyPath:@"name"];  过多的移除不会崩溃哟 
         
        //注销方式2
        [self.fbKVOController unobserve:self.t];
       // [self.fbKVOController unobserve:self.t]; 过多的移除不会崩溃哟 
        
        //注销方式3
        [self.fbKVOController unobserveAll];
        
        NSLog(@"%s",__FUNCTION__);
    }
    

    我们再细看初始化:方式3 和方式4(是否持有)

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIButton *jumpBtn = [UIButton new];
        jumpBtn.frame = CGRectMake(20, 100, 150, 80);
        [self.view  addSubview:jumpBtn];
        [jumpBtn setTitle:@"跳转VC" forState:UIControlStateNormal];
        [jumpBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [jumpBtn addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
        
         self.t  = [[TestModel alloc]init];
         
         
        //设置监听者为 self
        //持有
        self.fbKVOController = [[FBKVOController   alloc]initWithObserver:self retainObserved:YES];
        
        //不持有
        self.fbKVOController = [[FBKVOController   alloc]initWithObserver:self retainObserved:NO];
    
    }
    
    -(void)propertyHasChanged:(id)sender{
        NSLog(@"属性改变啦:%@",sender);
    }
    
    -(void)jump{
        KVOBaseUsesViewController_14_2* vc2 = [KVOBaseUsesViewController_14_2 new];
        vc2.vc1 = self;
        
        //设置监听的对象是vc2,监听的属性是vc1.t.name
        [self.fbKVOController observe:vc2 keyPath:@"vc1.t.name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld action:@selector(propertyHasChanged:)];
        [self.navigationController pushViewController:vc2 animated:YES];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.t.name = @"Jack";
            self.t.age = 16;
        });
    }
    -(void)dealloc{    
        //注销
        [self.fbKVOController unobserveAll];
        NSLog(@"%s",__FUNCTION__);
    }
    
    
    KVOBaseUsesViewController_14_2.h
    
    #import <UIKit/UIKit.h>
    #import "KVOBaseUsesViewController_14.h"
    
    @interface KVOBaseUsesViewController_14_2 : UIViewController
    @property (nonatomic,strong)KVOBaseUsesViewController_14  *vc1;
    @end
    
    KVOBaseUsesViewController_14_2.m
    #import "KVOBaseUsesViewController_14_2.h"
    
    @interface KVOBaseUsesViewController_14_2 ()
    @end
    
    @implementation KVOBaseUsesViewController_14_2
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
    -(void)dealloc{
        NSLog(@"%s",__FUNCTION__);
    }
    @end
    

    分析:
    1.当 设置retained的为YES,持有,则2个控制器都不会被释放(没有走dealloc的方法)
    2.当 设置retained的为NO,不持有,则2个控制器会被释放(走dealloc的方法)


    首先我们看下该框架的文件

    KVOController.h
    FBKVOController.h
    FBKVOController.m
    NSObject+FBKVOController.h
    NSObject+FBKVOController.m
    
    image
    image
    image
    image
    image
    image
    image
    image
    image
    image

    我们从上面的类分析有如下图关系:

    image

    具体的代码实现及说明:可参考写的gitHubDemo里面主要的步骤都加了注释

    友情链接:

    相关文章

      网友评论

          本文标题:三方库KVOController的使用及源码分析

          本文链接:https://www.haomeiwen.com/subject/gipvrqtx.html