iOS itself does not have a System checkbox component, but it is often used in actual development, so specifically write a CheckBox control, directly on the code
Effect Chart:
UICheckBoxButton.h files are as follows:
#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 files are as follows:
#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
How to use:
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];