ios中自定义button
自定义button
#import <UIKit/UIKit.h>
#define KFont 15 @interface DIYButton : UIButton @property(nonatomic,copy)NSString *ctrlName;
@end #import <UIKit/UIKit.h>
#define KFont 15 @interface DIYButton : UIButton @property(nonatomic,copy)NSString *ctrlName;
@end
自定义 view
@protocol FirstNavViewDelegate; @interface FirstNavView : UIView @property(nonatomic,retain)NSArray *data;
@property(nonatomic,assign)id<FirstNavViewDelegate> delegate;
- (id)initWithFrame:(CGRect)frame data:(NSArray *)adata;
@end @protocol FirstNavViewDelegate <NSObject> -(void)FirstNavViewClick:(DIYButton *)btn; @end #import "FirstNavView.h"
#import "InvoiceInfo.h"
#define KPadding 50
#define KMinTag 10 @implementation FirstNavView - (id)initWithFrame:(CGRect)frame data:(NSArray *)adata
{
self = [super initWithFrame:frame];
if (self) { CGFloat width=self.bounds.size.width/adata.count;
CGFloat height=self.bounds.size.height;
CGFloat center=width*0.5f;
for (int i=; i<adata.count; i++) {
DIYButton *btn=[[DIYButton alloc] init];
btn.bounds=CGRectMake(, , width-KPadding, height);
btn.center=CGPointMake(i*width+center, height*0.5f);
btn.tag=KMinTag+i;
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[btn release];
}
self.data=adata;//这一句是重点 }
return self;
} -(void)click:(DIYButton *)btn{
if (self.delegate &&[self.delegate respondsToSelector:@selector(FirstNavViewClick:)]) {
[self.delegate FirstNavViewClick:btn];
}
} -(void)setData:(NSArray *)data{
if (_data!=data) {
[_data release];
_data=[data retain];
for (int i=; i<data.count; i++) {
InvoiceInfo *info=data[i];
int tag=KMinTag+i;
UIView *view=[self viewWithTag:tag];
if ([view isKindOfClass:[DIYButton class]]) {
DIYButton *btn=(DIYButton *)view;
[btn setImage:[UIImage imageNamed:info.imagUrl] forState:UIControlStateNormal];
[btn setTitle:info.name forState:UIControlStateNormal];
btn.ctrlName=info.iPad_ctrls;
}
} }
} @end
使用 要实现
FirstNavViewDelegate这个协议
//一级导航
NSMutableArray *NavArrary=[NSMutableArray array];
path=[[NSBundle mainBundle]pathForResource:@"Pad_Menu_First" ofType:@"json"];
NSError *navError=nil;
content=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&navError];
if (navError) {
NSLog(@"读取%@错误",path);
return;
}
NSArray *arr=[content JSONValue];
for (NSDictionary *dic in arr) {
InvoiceInfo *info=[InvoiceInfo Invoice:dic];
[NavArrary addObject:info];
} FirstNavView *navFirst=[[FirstNavView alloc] initWithFrame:tpRec data:NavArrary];
navFirst.delegate=self;
[self.view addSubview:navFirst];
[navFirst release];
-(void)FirstNavViewClick:(DIYButton *)btn{
NSLog(@"一级导航__%@__%@",btn.titleLabel.text,btn.ctrlName);
[selfpushNavVc:btn.ctrlName];
}
ios中自定义button的更多相关文章
- ios中自定义checkbox
//自定义button#import <UIKit/UIKit.h> @interface CKButton : UIButton @end #import "CKButton. ...
- ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用
做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...
- ios中自定义cell 设置cell的分组结构
ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式 以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...
- iOS中 自定义cell分割线/分割线偏移 韩俊强的博客
在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...
- ios中自定义图层的2种方法
1:自定义图层,在图层中画图 #import <QuartzCore/QuartzCore.h> @interface MJLayer : CALayer @end #import &qu ...
- ios中自定义图层
图层和VIEW的区别 1:view不具备显示功能,是因view内部有一个图层,才能显示出来 2:图层不具备事件功能,VIEW继承UIRespone具有处理事件功能 3:自定义的图层有一个影式动画,VI ...
- iOS中自定义UITableViewCell的用法
1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...
- iOS 中自定义 cell,点击cell的时候文字不出现的原因
解决方案: 在setSelected方法中设置要显示label的背景颜色即可
- iOS中 自定义系统相机 作者:韩俊强
需要框架: #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> 布局如下 ...
随机推荐
- 浅析 @PathVariable 和 @RequestParam
一.代码实例 首先,上两个地址: 地址1:http://localhost:8989/SSSP/emps?pageNo=2 地址2:http://localhost:8989/SSSP/emp/7 如 ...
- 对Kalman(卡尔曼)滤波器的理解@@zz
1.简介(Brief Introduction) 在学习卡尔曼滤波器之前,首先看看为什么叫“卡尔曼”.跟其他著名的理论(例如傅立叶变换,泰勒级数等等)一样,卡尔曼也是一个人的名字,而跟他们不同的是,他 ...
- linux服务器上面部署ShowDoc 安装Composer
1.安装Composer Composer 是 PHP 的一个依赖管理工具,功能上类似于Java 的 Maven,Python 的 pip,Ruby的 gem,Nodejs 的 npm.详细介绍可参考 ...
- Android -- java代码设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- variable_scope和name_scope差别
先看代码: #命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用 import tensorflow as tf with t ...
- css文件放在根目录之后不起作用原因
修改为
- z-index的堆叠规则
原文: https://www.cnblogs.com/starof/p/4424926.html 对于所有定位,最后都不免遇到两个元素试图放在同一位置上的情况.显然,其中一个必须盖住另一个.但,如何 ...
- 【矩阵乘】【DP】【codevs 1305】Freda的道路
1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级: 大师 Master 题目描写叙述 Description Freda要到Rainbow的城堡去玩了. 我们能 ...
- Multiple Instance .NET Windows Service
It's not readily apparent how to install a Windows Service multiple times on a single machine. At fi ...
- jQuery对象
$(document).ready(function(){ //第二种获取方法,通过标签的名<h2>Dom来获取 var h1 = document.getElementsByTagNam ...