6.26 iOS开发Delegate使用

1.定义协议,定义方法,定义属性

#import <UIKit/UIKit.h>
#define kTitleViewH 50

@protocol MyOrderTopViewDelegate <NSObject>
@optional
-(void)didSelectedIndex:(NSInteger)tag;
@end
@interface MyOrderTitleView : UIView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray*)titles;
@property(nonatomic,weak) id<MyOrderTopViewDelegate> delegate;
@end



2.判断代理是否实现

#import "MyOrderTitleView.h"
#import "HWDConstant.h"
#import "UIViewExt.h"

@interface MyOrderTitleView ()
/** 标签栏底部的红色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, strong) NSArray *titlesViewArr;
@end
@implementation MyOrderTitleView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray*)titles{
    self=[super initWithFrame:frame];
    if (self) {
        self.titlesViewArr=titles;
        [self setupTitlesView:titles];
    }
    return self;
}

- (void)setupTitlesView:(NSArray*)titles
{
    //标签栏整体
    self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0];
    //下面的指示器view
    UIView *indicatorView = [[UIView alloc] init];
    self.indicatorView=indicatorView;
    indicatorView.backgroundColor = DTButtonColor;
    indicatorView.height = 2;
    indicatorView.tag = -1;
    indicatorView.y = kTitleViewH-1;
    [self addSubview:indicatorView];
    
    // 内部子视图空间
    CGFloat width = KScreenWidth / titles.count;
    CGFloat height = 50;
    for (NSInteger i = 0; i<titles.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        [button setTitleEdgeInsets:UIEdgeInsetsMake(5, 0, 0, 0)];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [button setTitleColor:DTButtonColor forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
            // 让按钮内部的label根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            indicatorView.width = width-30;
            indicatorView.centerX = button.centerX;
        }
    }
}
- (void)titleClick:(UIButton *)button
{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = KScreenWidth/self.titlesViewArr.count-30;
        self.indicatorView.centerX = button.centerX;
    }];
    
    // 滚动
//    CGPoint offset = self.contentView.contentOffset;
//    offset.x = button.tag * self.contentView.width;
//    [self.contentView setContentOffset:offset animated:YES];
    if ([self.delegate respondsToSelector:@selector(didSelectedIndex:)]) {
        [self.delegate didSelectedIndex:button.tag];
    }
    
}
@end



3.遵守协议,成为代理,实现方法

#import "MyOrderViewController.h"
#import "HWDConstant.h"
#import "MyOrderTitleView.h"

@interface MyOrderViewController ()<MyOrderTopViewDelegate>

@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor= DefaultColor;
    
    MyOrderTitleView * orderTitleView=[[MyOrderTitleView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, kTitleViewH) titles:@[@"全部订单",@"未完成",@"已完成"]];
    orderTitleView.delegate=self;
    [self.view addSubview:orderTitleView];
    
}

- (void)didSelectedIndex:(NSInteger)tag{
    NSLog(@"%ld",(long)tag);
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值