<01> - 代理<01>

———————————————Person.h<设置代理类的声明文件>------------------------------
 
#import <UIKit/UIKit.h>
//第一:
//1、引用类名
@class Person;
//2、定义一份代理协议
@protocol personDelegate <NSObject>
//3、声明代理方法
@required//必须实现的方法
- (void)personeat;
@optional//可选实现的方法(常用)
- (void)personplay;
- (void)personHabit:(NSString *)habit;

@end
@interface Person : UIView

//第二:声明代理属性(注意用weak)
@property (weak,nonatomic) id<personDelegate>delegate;

- (instancetype)initWithFrame:(CGRect)frame Titlearry:(NSArray *)titlearry;

@end
 
 
———————————————Person.m<设置代理类的实现文件>------------------------------
 
#import "Person.h"
@interface Person ()
@property (nonatomic,strong) UIButton *habitbutton;
@end
@implementation Person

-(instancetype)initWithFrame:(CGRect)frame Titlearry:(NSArray *)titlearry{
   
    self = [super initWithFrame:frame];
    if (self) {

       
        CGFloat interval = 20;
        CGFloat frameW = ((self.frame.size.width - (titlearry.count-1)*interval)/titlearry.count);
        CGFloat frameH = self.frame.size.height;
       
        for (int i = 0; i < titlearry.count; i++) {
           
            self.habitbutton = [UIButton buttonWithType:UIButtonTypeCustom];
            self.habitbutton.layer.borderColor = [UIColor blueColor].CGColor;
            self.habitbutton.layer.borderWidth = 1.0;
            self.habitbutton.frame = CGRectMake(i * (frameW + interval), 0,frameW, frameH);
            self.habitbutton.tag = 100+i;
            [self.habitbutton setTitle:titlearry[i] forState:UIControlStateNormal];
            [self.habitbutton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
           
            [self addSubview:self.habitbutton];
           
            [self.habitbutton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
 
           
        }
       
    }
   
   
    return self;
   
}

- (void)clickAction:(UIButton *)click{
   
   
    switch (click.tag) {
        case 100:
            [self personeatTitle:click.currentTitle];
            break;
        case 101:
            [self personplayTitle:click.currentTitle];
            break;
        case 102:
            [self peasonhabitTitle:click.currentTitle];
            break;
           
        default:
            break;
    }
   
}

//第三:调用代理的代理方法通知代理
- (void)personeatTitle:(NSString *)title{
   
    //调用代理的代理personeat通知代理
    [self.delegate personeat];
   
   
    //如果代理方法是@optional,那么需要判断方法是否有实现
    if ([self.delegate respondsToSelector:@selector(personHabit:)]) {
       
        [self.delegate personHabit:title];
    }
   

}
- (void)personplayTitle:(NSString *)title{
   
   
   
    //如果代理方法是@optional,那么需要判断方法是否有实现
    if ([self.delegate respondsToSelector:@selector(personplay)]) {
       
        [self.delegate personplay];
    }
   
    //如果代理方法是@optional,那么需要判断方法是否有实现
   
    if ([self.delegate respondsToSelector:@selector(personHabit:)]) {
       
        [self.delegate personHabit:title];
    }
   
 
}
- (void)peasonhabitTitle:(NSString *)title{
   
   
    //如果代理方法是@optional,那么需要判断方法是否有实现
   
    if ([self.delegate respondsToSelector:@selector(personHabit:)]) {
       
        [self.delegate personHabit:title];
    }
  
   
}


@end
 
———————————————ViewController.h<遵守代理类的声明文件>------------------------------
 
 
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end
———————————————ViewController.m<遵守代理类的实现文件>------------------------------
 
#import "ViewController.h"
#import "Person.h"
//第四:遵守代理协议
@interface ViewController ()<personDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
 
    Person *person = [[Person alloc] initWithFrame:CGRectMake(20, 64, self.view.frame.size.width-40, 45) Titlearry:[NSArray arrayWithObjects:@"畜生",@"动物",@"人类" ,nil]];
    //第五:设置代理对象
    person.delegate = self;
   
    [self.view addSubview:person];
   
   
    // Do any additional setup after loading the view, typically from a nib.
}
//第六:实现代理方法
#pragma markpersonDelegate

-(void)personeat{
   
    NSLog(@"只会吃....");
   
   
}

- (void)personplay{
   
     NSLog(@"只会玩....");
   
}
- (void)personHabit:(NSString *)habit{
   
    NSLog(@"这是:%@....",habit);
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end
 
 
 
 
 
posted @ 2017-04-13 14:33  NextXavier  阅读(161)  评论(0)    收藏  举报