KVO键值观察机制实例-添加删除星座按钮

来源:互联网 发布:java机房管理系统 编辑:程序博客网 时间:2024/06/03 21:43

KVO:Key-Value Observing

每次指定的被观察者的对象属性改变后,KVO自动通知相应的观察者。

与代理模式有异曲同工之妙。

实现步骤:

1.注册 确定观察者,被观察者着,和指定的被观察者的属性

2.实现回调方法(在观察者类中实现)

3.移除观察 由被观察者调用

观察者

//  ViewController.m

//  907重做图片排列



#import "ViewController.h"

#import "SecondViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define SPACE_SMALL 40

#define IMAGE_SIZE 40

@interface ViewController ()

{

    UISegmentedControl *sortSegment;//排列分段

    NSMutableArray *btnAry;//按钮数组

    SecondViewController *nextView;//下一页

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    nextView=[[SecondViewControlleralloc] init];


    //添加分段

    [selfaddSortSegment];

   //添加默认的按钮

    [selfaddSolarBtns];

    sortSegment.selectedSegmentIndex=1;

    [selfclickSegment:sortSegment];

    //下一页按钮

    [selfaddToNextBtn];

    //1.注册

    [nextViewaddObserver:selfforKeyPath:@"tag"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:nil];

   

    

}

#pragma mark-2.回调函数

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    NSLog(@"KVO回调方法。。。。。");

//当被观察者,被观察的属性有多个时,需要根据被观察对象,被观察属性进行判断

    if ([objectisEqual:nextView]) {

        if ([keyPathisEqualToString:@"tag"] ) {

            NSUInteger tag=[[changeobjectForKey:@"new"]integerValue];


            if (tag==1) {

                //

                [selfaddOneFace];

            }

            if(tag==2){

                [selfsubOneFace];


            }

        }

    }


}

#pragma mark-3.移除观察

-(void)dealloc{

    [selfremoveObserver:nextView forKeyPath:@"tag"];

}

#pragma mark-添加一个表情到数组

-(void)addOneFace{

    UIButton *btn=[[UIButtonalloc] initWithFrame:CGRectMake(0,0,IMAGE_SIZE, IMAGE_SIZE)];

    [btn setBackgroundImage:[UIImageimageNamed: [NSStringstringWithFormat:@"icon%i.gif",arc4random()%12+1]] forState:UIControlStateNormal];

    [btnAryaddObject:btn];

    [self.viewaddSubview:btn];

    [selfclickSegment:sortSegment];


}

#pragma mark-删除一个表情

-(void)subOneFace{

    UIButton *btn=[btnAryobjectAtIndex:arc4random()%btnAry.count];

    NSLog(@"last:%lu",btnAry.count);

    [btnAryremoveObject:btn];

    NSLog(@"删除后:%lu",btnAry.count);

    [btn removeFromSuperview];

     [selfclickSegment:sortSegment];

    

}

#pragma mark-添加跳转到下一页的按钮

-(void)addToNextBtn{

    UIButton *nextBnt=[[UIButtonalloc] initWithFrame:CGRectMake(SCREEN_WIDTH-SPACE_SMALL*3,SCREEN_HEIGHT-2*SPACE_SMALL, SPACE_SMALL*2, SPACE_SMALL)];

    [nextBnt setTitle:@"下一页" forState:UIControlStateNormal];

    nextBnt.backgroundColor=[UIColorredColor];

    

    [self.viewaddSubview:nextBnt];

    [nextBnt addTarget:self action:@selector(clickTonextBtn:) forControlEvents:UIControlEventTouchUpInside];

    


}

#pragma mark-下一页按钮监听

-(void)clickTonextBtn:(UIButton *)sender{

    self.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

    [selfpresentViewController:nextView animated:YES completion:nil];


}

#pragma mark-排列

-(void)clickSegment:(UISegmentedControl *)sender{

    //列数

    NSUInteger line=sender.selectedSegmentIndex+2;

    //宽度间隙

    float space=(SCREEN_WIDTH-line*IMAGE_SIZE)/(line+1);

    for (int i=0; i<btnAry.count; i++) {

        UIButton *btn=[btnAryobjectAtIndex:i];

        int row=i/line;//所在行

        int col=i%line;//所在列

        float xx=space+(space+IMAGE_SIZE)*col;

        float yy=CGRectGetMaxY(sortSegment.frame)+SPACE_SMALL+ (SPACE_SMALL/4+IMAGE_SIZE)*row;

        [UIViewanimateWithDuration:0.5 animations:^{

            btn.frame=CGRectMake(xx, yy, IMAGE_SIZE, IMAGE_SIZE);

        }];

        

        

    }


}

#pragma mark-添加默认的星座按钮

-(void)addSolarBtns{

    btnAry=[NSMutableArrayarray];

    for (int i=0; i<12; i++) {

        

        UIButton *btn=[[UIButtonalloc] initWithFrame:CGRectMake(SPACE_SMALL,CGRectGetMaxY(sortSegment.frame)+SPACE_SMALL+ (SPACE_SMALL/4+IMAGE_SIZE)*i,IMAGE_SIZE, IMAGE_SIZE)];

        [btn setBackgroundImage:[UIImageimageNamed: [NSStringstringWithFormat:@"icon%i.gif",i+1]] forState:UIControlStateNormal];

        [self.viewaddSubview:btn];

        [btnAryaddObject:btn];

//        NSLog(@"%f",CGRectGetMaxY(sortSegment.frame));

    }


}

#pragma mark-添加

-(void)addSortSegment{

    sortSegment=[[UISegmentedControlalloc] initWithItems:@[@"两列",@"三列",@"四列",@"五列"]];

    sortSegment.frame=CGRectMake(SPACE_SMALL,SPACE_SMALL,240,SPACE_SMALL);

    sortSegment.tintColor=[UIColorredColor];

    

    [self.viewaddSubview:sortSegment];

    sortSegment.selectedSegmentIndex=1;

    [sortSegmentaddTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];

   

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


被观察者

//

//  SecondViewController.m

//  907重做图片排列

//

//  Created by jerehedu on 16/9/7.

//  Copyright © 2016 jerehedu. All rights reserved.

//


#import "SecondViewController.h"


@interface SecondViewController ()


@end


@implementation SecondViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

}

#pragma mark-返回按钮监听

- (IBAction)clickToBack:(UIButton *)sender {

    [self dismissViewControllerAnimated:YEScompletion:nil];

}

#pragma mark-添加或者删除表情

- (IBAction)clickTochangeImageCount:(UIButton *)sender {

    [self setValue:@(sender.tag)forKey:@"tag"];

    NSLog(@"tag=%lu",self.tag);

    [self dismissViewControllerAnimated:YEScompletion:nil];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end





0 0
原创粉丝点击