iOS Development Custom CheckBox control

Source: Internet
Author: User
Tags bool uikit

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];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.