IOS开发自定义CheckBox控件
来源:易贤网 阅读:2294 次 日期:2014-11-04 11:30:51
温馨提示:易贤网小编为您整理了“IOS开发自定义CheckBox控件”,方便广大网友查阅!

IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码

效果图:

名单

UICheckBoxButton.h文件如下:

#import

#import "Common.h"

@interface UICheckBoxButton : UIControl

{

UILabel *label;

UIImageView *icon;

BOOL checked;

id delegate;

}

@property (retain, nonatomic) id delegate;

@property (retain, nonatomic) UILabel *label;

@property (retain, nonatomic) UIImageView *icon;

-(BOOL)isChecked;

-(void)setChecked: (BOOL)flag;

@end

UICheckBoxButton.m文件如下:

#import "UICheckBoxButton.h"

@implementation UICheckBoxButton

@synthesize label,icon,delegate;

- (id)initWithFrame:(CGRect)frame {

if ( self = [super initWithFrame: frame])

{

icon =[[UIImageView alloc] initWithFrame: CGRectMake (0, 0, frame.size.height, frame.size.height)];

[self setChecked:NO];

[self addSubview: icon];

label =[[UILabel alloc] initWithFrame: CGRectMake(icon.frame.size.width + 7, 0,

frame.size.width - icon.frame.size.width - 10,

frame.size.height)];

label.backgroundColor =[UIColor clearColor];

label.textAlignment = UITextAlignmentLeft;

[self addSubview:label];

[self addTarget:self action:@selector(clicked) forControlEvents: UIControlEventTouchUpInside];

}

return self;

}

-(BOOL)isChecked {

return checked;

}

-(void)setChecked: (BOOL)flag {

if (flag != checked)

{

checked = flag;

}

if (checked)

{

[icon setImage: [UIImage imageNamed:@"checkBoxSelect.png"]];

}

else

{

[icon setImage: [UIImage imageNamed:@"checkBoxNoSelect.png"]];

}

}

-(void)clicked {

[self setChecked: !checked];

if (delegate != nil)

{

SEL sel = NSSelectorFromString (@"checkButtonClicked");

if ([delegate respondsToSelector: sel])

{

[delegate performSelector: sel];

}

}

}

-(void)dealloc {

delegate = nil;

[label release];

[icon release];

[super dealloc];

}

@end

使用方法:

UICheckBoxButton *checkBoxButton = [[ UICheckBoxButton alloc] initWithFrame: CGRectMake(30, 50, 220, 25)];

checkBoxButton.delegate = self.delegate;

checkBoxButton.label.text = [Common getTextByTag:@"noCostPrompt"];

checkBoxButton.label.textColor = [Common getColorByTag:@"alertLabelcolor"];

[self.view addSubview:checkBoxButton];

[checkBoxButton release];

更多信息请查看IT技术专栏

更多信息请查看技术文章
易贤网手机网站地址:IOS开发自定义CheckBox控件
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65099533/13759567129 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标