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