代理(超级能理解的自己的方式)

1:在控制器里面有一个自己增加的view,view里面有一个button,然后button点击之后,想要在整个的控制器的view里面显示出一个label出来。

2:主要分成几步:

      第一明确,谁发起了动作给谁,如果A发起了动作给B,那么B就是A的代理。

      A里面需要:(1)实现一个协议  ,并且提供一个方法  (2)设置一个weak属性的delegate变量  (3)在A里面一定要先判断下代理即B有没有实现对应协议里面的方法

                          (4)如果B实现了A提供的协议里面的方法,那么A调用协议里面的方法。

      B里面需要:(1)定义一个A类型的变量 ,设置A.delegate = self; (2)用尖括号遵循A的协议 (3)实现A中的协议里面定义的变量。

      注意命名规范~~~在李明杰的3月份靠后的视频里面有讲解

3:代码演示:appView就是A,A中的按钮点击了之后,要再controller里面显示一个label,controller就是B

   

//
//  MJAppView.h
//  03-应用管理
//
//  Created by MJ Lee on 14-3-14.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>
@class MJApp;
@class MJAppView;

//声明一个协议
@protocol MJAppViewDelegate <NSObject>
@optional
-(void)appViewClickedDownloadButton:(MJAppView *)appView;

@end
@interface MJAppView : UIView
@property(nonatomic,weak) id<MJAppViewDelegate>delegate;

@property (strong, nonatomic) MJApp *app;
@end


A中调用A自己声明,但是在是在B中实现的方法:

-(void)download:(UIButton *)btn
{
    btn.enabled = NO;
    [btn  setTitle:@"已下载" forState:UIControlStateNormal];
    
    if ([self.delegate respondsToSelector:@selector( appViewClickedDownloadButton:)]) {
        [self.delegate appViewClickedDownloadButton:self];
    }
    //NSLog(@"name is %@",self.app.name);
}

B里面:

//
//  MJViewController.m
//  03-应用管理
//
//  Created by MJ Lee on 14-3-13.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "MJApp.h"
#import "MJAppView.h"

<span style="background-color: rgb(255, 0, 0);">@interface MJViewController ()<MJAppViewDelegate></span>
@property (nonatomic, strong) NSArray *apps;
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.基本参数
    CGFloat appViewW = 90;
    CGFloat appViewH = 100;
    int columns = 3;
    CGFloat paddingX = (self.view.frame.size.width - columns * appViewW) / (columns + 1);
    CGFloat paddingY = 10;
    
    // 2.遍历所有的数据
    int count = self.apps.count;
    for (int i = 0; i<count; i++) {
        MJAppView *appView = [[MJAppView alloc] init];
       <span style="color:#ff0000;"> appView.delegate = self;</span>
        int row = i / columns;
        int col = i % columns;
        CGFloat appViewY = 30 + row * (paddingY + appViewH);
        CGFloat appViewX = paddingX + col * (paddingX + appViewW);
        appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
        appView.app = self.apps[i];
        [self.view addSubview:appView];
    }
}
<span style="color:#ff0000;">//当点击了下载按钮时就会调用
-(void)appViewClickedDownloadButton:(MJAppView *)appView
{
    UILabel *label = [[UILabel alloc]init];
    MJApp *app = appView.app;
   
    label.text = [NSString stringWithFormat:@"成功下载%@", app.name];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor blackColor];
    label.frame = CGRectMake(0, 0, 150, 25);
    label.center = CGPointMake(160, 240);
    label.alpha = 0.0;
    label.layer.cornerRadius = 5;
    label.clipsToBounds = YES;
    
    [self.view addSubview:label];
    
    //动画
    [UIView animateWithDuration:1.0 animations:^{
        label.alpha = 0.5;
    }completion:^(BOOL finished){
        [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
            label.alpha = 0.0;
        } completion:^(BOOL finished) {
           
            [label removeFromSuperview];
        }];
    }];
}</span>

- (NSArray *)apps
{
    if (_apps == nil) {
        // 1.加载plist
        NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
        
        // 2.创建模型数组
        NSMutableArray *appArray = [NSMutableArray array];
        for (NSDictionary *dict in tempArray) {
            MJApp *app = [MJApp appWithDict:dict];
            [appArray addObject:app];
        }
        
        // 3.赋值
        _apps = appArray;
    }
    return _apps;
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值