标题 | 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]; |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。