日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

自定義控件復(fù)選框和單選框的實(shí)現(xiàn)

系統(tǒng) 1892 0

我們先實(shí)現(xiàn)單個(gè)按鈕,為了復(fù)用,不管單選還是復(fù)選按鈕都是使用同一個(gè)類來實(shí)現(xiàn),為了區(qū)別單選還是復(fù)選,我們用一個(gè)自定義枚舉類型 CheckButtonStyle屬性style來區(qū)別,當(dāng)其值設(shè)置為CheckButtonStyleDefault或CheckButtonStyleBox時(shí),為復(fù)選按鈕:

當(dāng)其值設(shè)為 CheckButtonStyleRadio時(shí),為單選按鈕:

當(dāng)按鈕在選中/反選狀態(tài)間切換時(shí) ,文字左邊的圖片自動(dòng)轉(zhuǎn)換。

整個(gè)控件是由一個(gè) ImageView、一個(gè)Label、一個(gè)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 ; // 勾選/反選時(shí)的圖片文件名

}

@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

具體實(shí)現(xiàn)如下:

#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 ]; // 默認(rèn)風(fēng)格為方框(多選)樣式

//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類很簡單,構(gòu)造、設(shè)置標(biāo)簽文本等屬性,然后addSubview:

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

二、單選按鈕組的實(shí)現(xiàn)

復(fù)選按鈕無所謂“組”的概念,單選按鈕則不同。在同一個(gè)組中,單選按鈕只允許同時(shí)選擇一個(gè)按鈕,不能選多個(gè),因此我們要實(shí)現(xiàn)一個(gè)單選按鈕組的類:

#import <Foundation/Foundation.h>

#import "CheckButton.h"

@interface RadioGroup : NSObject {

NSMutableArray * children ;

NSString * text ;

id value ;

}

@property ( readonly )NSString* text;

@property ( readonly ) id value;

-( void )add:( CheckButton *)cb;

-( void )checkButtonClicked:( id )sender;

@end

#import "RadioGroup.h"

@implementation RadioGroup

@synthesize text,value;

-( id )init{

if ( self =[ super init ]){

children =[[ NSMutableArray alloc ] init ];

}

return self ;

}

-( void )add:( CheckButton *)cb{

cb. delegate = self ;

if (cb. checked ) {

text =cb. label . text ;

value =cb. value ;

}

[ children addObject :cb];

}

-( void )checkButtonClicked:( id )sender{

CheckButton * cb=( CheckButton *)sender;

if (!cb. checked ) {

// 實(shí)現(xiàn)單選

for ( CheckButton * each in children ){

if (each. checked ) {

[each setChecked : NO ];

}

}

[cb setChecked : YES ];

// 復(fù)制選擇的項(xiàng)

text =cb. label . text ;

value =cb. value ;

}

NSLog ( @"text:%@,value:%d" , text ,[( NSNumber *) value intValue ]);

}

-( void )dealloc{

[ text release ];

value = nil ;

[ children release ];

[ super dealloc ];

}

@end

單選按鈕組在 ViewController中的使用:

-( id )initWithNibName:( NSString *)nibNameOrNil bundle:( NSBundle *)nibBundleOrNil{

if ( self =[ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil]){

// 單選按鈕組

rg =[[ RadioGroup alloc ] init ];

// 1 個(gè)單選按鈕

CheckButton * cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )];

// 把單選按鈕加入按鈕組

[ rg add :cb];

cb. label . text = @"★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 1 ];

// 把按鈕設(shè)置為單選按鈕樣式

cb. style = CheckButtonStyleRadio ;

// 加入視圖

[ self . view addSubview :cb];

[cb release ]; //add 后,會(huì)自動(dòng)持有,可以釋放

// 2 個(gè)單選按鈕

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 100 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 2 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

// 3 個(gè)單選按鈕

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 140 , 260 , 32 )];

// 各種屬性必須在 [rg addv] 之前設(shè)置,否則 text value 不會(huì)被 populate

cb. checked = YES ;

cb. label . text = @"★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 3 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[ rg add :cb]; // 屬性設(shè)置完之后再 add

[cb release ];

// 4 個(gè)單選按鈕

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 180 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 4 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

// 5 個(gè)單選按鈕

cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 220 , 260 , 32 )];

[ rg add :cb];

cb. label . text = @"★★★★★" ;

cb. value =[[ NSNumber alloc ] initWithInt : 5 ];

cb. style = CheckButtonStyleRadio ;

[ self . view addSubview :cb];

[cb release ];

}

return self ;

}

運(yùn)行效果:

自定義控件復(fù)選框和單選框的實(shí)現(xiàn)

自定義控件復(fù)選框和單選框的實(shí)現(xiàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 五华县| 共和县| 田阳县| 墨玉县| 灵宝市| 山阴县| 盘锦市| 常德市| 固始县| 乡城县| 平果县| 宁远县| 方山县| 通州区| 金湖县| 故城县| 称多县| 隆安县| 南宫市| 崇阳县| 乐清市| 凭祥市| 达尔| 青海省| 洪湖市| 曲麻莱县| 漳浦县| 冕宁县| 花莲县| 富锦市| 萝北县| 武穴市| 嵩明县| 乐亭县| 扎兰屯市| 平原县| 黄浦区| 阜平县| 伽师县| 策勒县| 微山县|