• Day10


    1 delegate 代理

      

    @protocol AppInfoViewClassDelegate <NSObject>
    
    -(void)appInfoViewDownLoad:(AppInfoViewClass *)appInfoView;
    
    @end
    
    
    @property (nonatomic,weak) id<AppInfoViewClassDelegate> delegate; 
    
    
     if ([self.delegate respondsToSelector:@selector(appInfoViewDownLoad:)]) {
            [self.delegate appInfoViewDownLoad:self];
        }
    

    实践中怎么调用可参考

    //
    //  AppInfoViewClass.h
    //  AppManager2
    //
    //  Created by xin on 15-3-19.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "AppObject.h"
    @class AppInfoViewClass;
    
    @protocol AppInfoViewClassDelegate <NSObject>
    
    -(void)appInfoViewDownLoad:(AppInfoViewClass *)appInfoView;
    
    @end
    
    
    @interface AppInfoViewClass : UIView
    
    @property (nonatomic,weak) id<AppInfoViewClassDelegate> delegate; 
    
    //@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
    
    @property (nonatomic,strong) AppObject *appObject;
    
    +(instancetype)initAppInfoViewWithDict:(AppObject *)appObject;
    
    @end
    

      

    //
    //  AppInfoViewClass.m
    //  AppManager2
    //
    //  Created by xin on 15-3-19.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "AppInfoViewClass.h"
    
    @interface AppInfoViewClass()
    @property (weak, nonatomic) IBOutlet UIImageView *iconView;
    @property (weak, nonatomic) IBOutlet UILabel *descriptionLabel;
    
    @end
    
    @implementation AppInfoViewClass
    
    -(void)setAppObject:(AppObject *)appObject{
        _appObject = appObject;
        self.iconView.image = appObject.image;
        self.descriptionLabel.text = appObject.iconDescription;
    }
    
    +(instancetype)getXibView{
        NSArray *appArray = [[NSBundle mainBundle]loadNibNamed:@"AppInfoView" owner:nil options:nil];
        return [appArray firstObject];
    }
    
    +(instancetype)initAppInfoViewWithDict:(AppObject *)appObject{
        AppInfoViewClass *view = [self getXibView];
        view.appObject = appObject;
        return view;
    }
    
    - (IBAction)downloadClick {
        //让代理办事
        if ([self.delegate respondsToSelector:@selector(appInfoViewDownLoad:)]) {
            [self.delegate appInfoViewDownLoad:self];
        }
    }
    
    
    
    
    @end
    

      

    //
    //  ViewController.m
    //  AppManager2
    //
    //  Created by xin on 15-3-18.
    //  Copyright (c) 2015年 Jackey. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "AppObject.h"
    #import "AppInfoViewClass.h"
    
    @interface ViewController ()<AppInfoViewClassDelegate>
    
    @property (nonatomic,strong) NSArray *appList;
    
    @end
    
    @implementation ViewController
    
    -(NSArray *)appList{
        if(!_appList){
            NSString *path = [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
            NSArray *array = [[NSArray alloc]initWithContentsOfFile:path];
            NSMutableArray *arrayM = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                AppObject *appObject = [[AppObject alloc]initWithDict:dict];
                [arrayM addObject:appObject];
            }
            _appList = arrayM;
        }
        return _appList;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        int totalColumn = 3;
        CGFloat viewW = 80;
        CGFloat viewH = 90;
        
        CGFloat marginX = ((self.view.bounds.size.width - viewW * totalColumn) / (totalColumn + 1));
        CGFloat marginY = 10;
        CGFloat ViewY = 20;
    
        
        for (int i=0; i<self.appList.count; i++) {
            
            
            //绘制view
            //1 2 3 4 5 6
            CGFloat row = i / totalColumn;
            CGFloat col = i % totalColumn;
            
            CGFloat x = (viewW + marginX) * col + marginX;
            CGFloat y = (viewH + marginY) * row + marginY + ViewY;
            
            AppInfoViewClass *view = [AppInfoViewClass initAppInfoViewWithDict:self.appList[i]];
            view.delegate = self;
            view.frame = CGRectMake(x, y, viewW, viewH);
            [self.view addSubview:view];
            
            
        }
        
    }
    
    -(void)appInfoViewDownLoad:(AppInfoViewClass *)appInfoView{
        
        UILabel *alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 400, 160, 40)];
        alertLabel.text =appInfoView.appObject.iconDescription;
        alertLabel.textAlignment = NSTextAlignmentCenter;
        alertLabel.backgroundColor = [UIColor grayColor];
        [self.view addSubview:alertLabel];
        alertLabel.alpha = 1.0;
        
        [UIView animateWithDuration:1.0 animations:^{
            alertLabel.alpha = 0.0;
        } completion:^(BOOL finished) {
            [alertLabel removeFromSuperview];
        }];
    
    }
    @end
    

      

  • 相关阅读:
    WPF自定义路由事件
    一文看懂微服务背后的技术演进与应用实践
    云原生消息、事件、流超融合平台——RocketMQ 5.0 初探
    KubeVela 1.1 发布,开启混合环境应用交付新里程碑
    Facebook宕机背后,我们该如何及时发现DNS问题
    Apache Flink 在汽车之家的应用与实践
    新一代容器平台ACK Anywhere,来了
    Serverless 工程实践 | Serverless 应用优化与调试秘诀
    OpenKruise 如何实现应用的可用性防护?
    国家网络安全宣传周:勒索病毒利如刀,上网备好技能包
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4404370.html
Copyright © 2020-2023  润新知