现在的位置: 首页 > 综合 > 正文

ios自定义控件复选框和单选框的实现

2013年10月25日 ⁄ 综合 ⁄ 共 2712字 ⁄ 字号 评论关闭

我们先实现单个按钮,为了复用,不管单选还是复选按钮都是使用同一个类来实现,为了区别单选还是复选,我们用一个自定义枚举类型CheckButtonStyle属性style来区别,当其值设置为CheckButtonStyleDefault或CheckButtonStyleBox时,为复选按钮:

  当其值设为CheckButtonStyleRadio时,为单选按钮:

当按钮在选中/反选状态间切换时,文字左边的图片自动转换。

整个控件是由一个ImageView、一个Label、一个BOOL变量及其他变量组成,.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

,

delegate

;

CheckButtonStyle

style

;

NSString

*

checkname

,*

uncheckname

;

//

勾选/反选时的图片文件名

}

@property

(

retain

,

nonatomic

)

id

value,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

具体实现如下:

#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类很简单,构造、设置标签文本等属性,然后addSubview:

CheckButton

* cb=[[

CheckButton

a

lloc

]
initWithFrame

:

CGRectMake

(

20

,
60

,
260

,
32

)];

cb.

label

.

text

=

@"checkbutton1"

;

cb.

value

=[[

NSNumber

alloc

]

initWithInt

:

18

];

cb.

style

=

抱歉!评论已关闭.