ios开发自定义checkbox控件
来源: 阅读:877 次 日期:2014-11-18 11:16:45
温馨提示: 小编为您整理了“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控件
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map