6.26 iOS开发Delegate使用
来源:互联网 发布:瞻博网络待遇 编辑:程序博客网 时间:2024/06/06 07:40
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
阅读全文
0 0
- 6.26 iOS开发Delegate使用
- iOS开发Delegate,Notification,Block使用心得
- iOS开发-Delegate 代理 使用和注意事项
- IOS开发使用委托delegate在…
- iOS Delegate 使用五步曲
- iOS Delegate 的使用
- iOS delegate 的使用
- iOS delegate使用
- iOS开发知识点汇集三 —— Delegate的使用
- ios开发笔记--关于delegate(委托)的使用心得
- 浅谈IOS开发中委托(Delegate)的使用
- iOS开发——使用delegate进行消息传递
- ios开发笔记--关于delegate(委托)的使用心得
- iOS 开发学习之 delegate
- iOS开发 -- delegate 设计模式
- iOS 彻底学会使用delegate
- iOS 彻底学会使用delegate
- iOS 彻底学会使用delegate
- 找出链表中是否存在环
- Unity Debug
- MMU机制在进程中使用时的一些问题
- 将字符串转换为整数
- 在centos6.6上安装运行docker
- 6.26 iOS开发Delegate使用
- 【Unity技巧】四元数(Quaternion)和旋转
- ios 写项目的时候遇到的问题及解决方案
- 设置qq在线客服 并跟随页面滚动
- redis 3.2.8主从HA搭建
- 引入XSD的两种方式
- win10 软件无法启动,启动奔溃解决办法
- lombok在IntelliJ IDEA下的使用
- ubuntu+anaconda+opencv报错