整个控件是由一个ImageView、一个Label、一个BOOL变量及其他变量组成,CheckButton.h文件如下: typedef enum { CheckButtonStyleDefault = 0 , CheckButtonStyleBox = 1 , CheckButtonStyleRadio = 2 } CheckButtonStyle; #import <Foundation/Foundation.h> @interface CheckButton : UIControl { //UIControl* control; UILabel *label; UIImageView *icon; BOOL checked; id value; id delegate; CheckButtonStyle style; NSString *checkname; NSString *uncheckname; // 勾选/反选时的图片文件名 } @property ( retain , nonatomic ) id value; @property ( retain , nonatomic ) id delegate; @property ( retain , nonatomic ) UILabel *label; @property ( retain , nonatomic ) UIImageView *icon; @property ( assign ) CheckButtonStyle style; -( CheckButtonStyle )style; -( void )setStyle: ( CheckButtonStyle )st; -( BOOL )isChecked; -( void )setChecked: ( BOOL )b; @end
CheckButton.m如下: #import "CheckButton.h" @implementation CheckButton @synthesize label,icon,value,delegate; -( id )initWithFrame: ( CGRect )frame { if ( self = [super initWithFrame: frame]) { icon =[[UIImageView alloc] initWithFrame: CGRectMake ( 10 , 0 , frame.size.height , frame.size.height )]; [ self setStyle : CheckButtonStyleDefault ]; // 默认风格为方框(多选)样式 //self.backgroundColor=[UIColor grayColor]; [ self addSubview : icon ]; label =[[ UILabel alloc ] initWithFrame : CGRectMake ( icon.frame.size.width + 24 , 0 , frame.size.width - icon.frame.size.width - 24 , frame.size.height )]; label.backgroundColor =[ UIColor clearColor ]; label.font =[ UIFont fontWithName : @"Arial" size : 20 ]; label.textColor =[ UIColor colorWithRed : 0xf9 / 255.0 green : 0xd8 / 255.0 blue : 0x67 / 255.0 alpha : 1 ]; label.textAlignment = UITextAlignmentLeft; [self addSubview: label]; [self addTarget: self action: @selector( clicked ) forControlEvents: UIControlEventTouchUpInside]; } return self; } -( CheckButtonStyle )style { return style ; } -( void )setStyle: ( CheckButtonStyle )st { style =st; switch ( style ) { case CheckButtonStyleDefault: case CheckButtonStyleBox: checkname = @"checked.png"; uncheckname = @"unchecked.png"; break; case CheckButtonStyleRadio: checkname = @"radio.png"; uncheckname = @"unradio.png"; break; default: break; } [self setChecked: checked]; } -( BOOL )isChecked { return checked; } -( void )setChecked: ( BOOL )b { if (b!= checked ) { checked = b; } if ( checked ) { [icon setImage: [UIImage imageNamed: checkname]]; } else { [icon setImage: [UIImage imageNamed: uncheckname]]; } } -( void )clicked { [self setChecked: !checked]; if ( delegate != nil ) { SEL sel= NSSelectorFromString ( @"checkButtonClicked" ); if ([delegate respondsToSelector: sel]) { [delegate performSelector: sel]; } } } -( void )dealloc{ value = nil ; delegate = nil ; [ label release ]; [ icon release ]; [ super dealloc ]; } @end
使用自定义按钮: CheckButton * cb=[[ CheckButton a lloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )]; cb. label . text = @"checkbutton1" ; cb. value =[[ NSNumber alloc ] initWithInt : 18 ]; cb. style = CheckButtonStyleDefault ; [ self . view addSubview :cb];
本文介绍了一个自定义的CheckButton控件实现方法,该控件由图片、标签及布尔变量组成,支持三种样式:默认多选方框、单选框和圆形单选按钮,并详细展示了CheckButton.h和CheckButton.m文件的代码。

被折叠的 条评论
为什么被折叠?



