多選框的Demo

本Demo參照網絡資源。

下載地址:http://download.csdn.net/detail/zgcrichard/5267065

在開發IOS項目過程中會有多選的需求,雖然IOS提供了UISwitch可以代替多選框,但是有時候還是會懷念C#、JAVA中的多選框,本Demo幫助大家實現了此功能,本Demo實現了界面展示值和實際值之間的輕鬆獲取(例如:界面上展示:是否添加,而後臺很有可能就是一個標誌符0,1 or 2...)。有需要的可直接拿去,可直接放到項目中使用。本人水平有限紕漏錯誤在所難免,可以留言建議。


//
//  UICheckBoxButton.h
//  UICheckBox
//

#import <UIKit/UIKit.h>

@interface UICheckBoxButton : UIControl{

    UILabel *label;

    UIImageView *icon;

    BOOL checked;

    id delegate;    

    NSArray *arr_code;

}


@property (nonatomic,retain)id delegate;

@property (nonatomic,retain)UILabel *label;

@property (nonatomic,retain)UIImageView *icon;

@property (nonatomic,retain)NSArray *arr_code;


-(BOOL)isChecked;

-(void)setChecked:(BOOL)flag;

//顯示內容和實際Value

-(void)setValue:(NSString *)Value withArrCode:(NSArray *)ArrCode;

//返回實際Value

-(NSString *)getCode;


@end





//
//  UICheckBoxButton.m
//  UICheckBox
//


#import "UICheckBoxButton.h"


@implementation UICheckBoxButton

@synthesize label,icon,delegate,arr_code;


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        icon = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, frame.size.height, frame.size.height)];

        [self setChecked:NO];

        [selfaddSubview:icon];

        label = [[UILabelalloc]initWithFrame:CGRectMake(icon.frame.size.width+7,0, frame.size.width-icon.frame.size.width-10, frame.size.height)];

        label.backgroundColor = [UIColorclearColor];

        label.textAlignment =UITextAlignmentLeft;

        

        [selfaddSubview:label];

        [selfaddTarget:selfaction:@selector(clicked)forControlEvents:UIControlEventTouchUpInside];

        

    }

    return self;

}


-(BOOL)isChecked{

    returnchecked;

}


-(void)setChecked:(BOOL)flag{

    if (flag != checked) {

        checked = flag;

    }

    if (checked) {

        [icon setImage:[UIImageimageNamed:@"Check1.png"]];

    }else{

        [icon setImage:[UIImageimageNamed:@"UnCheck1.png"]];

    }

}

//複選框選擇事件

-(void)clicked{

    [selfsetChecked:!checked];

    if (delegate !=nil) {

        SEL sel =NSSelectorFromString(@"checkButtonClicked");

        if ([delegaterespondsToSelector:sel]) {

            [delegate performSelector:sel];

        }

    }

}

//Value是顯示的Label arrCode是兩個值,index0:選擇;index1:不選擇

-(void)setValue:(NSString *)Value withArrCode:(NSArray *)ArrCode{

    NSCharacterSet *space = [NSCharacterSetwhitespaceCharacterSet];

    NSString *trimmValue = [Value stringByTrimmingCharactersInSet:space];

    if (ArrCode.count !=2) {

        UIAlertView *alert = [[UIAlertViewalloc]

                    initWithTitle:@"提示"

                    message:@"UICheckBox_Value"

                    delegate:nil

                    cancelButtonTitle:@"確定"

                    otherButtonTitles: nil];

        [alert show];

        [alert release];

        return;

    }

    NSString *Check = [ArrCode objectAtIndex:0];

    NSString *UnCheck = [ArrCode objectAtIndex:1];

    NSString *trimmCheckCode = [Check stringByTrimmingCharactersInSet:space];

    NSString *trimmUnCheckCode = [UnCheck stringByTrimmingCharactersInSet:space];

    if ((trimmCheckCode.length ==0 || trimmUnCheckCode.length ==0) && trimmValue.length == 0) {

        return;

    }else{

        self.label.text = Value;

        self.arr_code = ArrCode;

    }

}


-(NSString *)getCode{

    NSString *code = nil;

    if (checked) {

        code = [self.arr_codeobjectAtIndex:0];

    }else{

        code = [self.arr_codeobjectAtIndex:1];

    }

    return code;

}


-(void)dealloc{

    delegate  = nil;

    [label release];

    [icon release];

    [arr_code release];

    [super dealloc];

}


@end



//  使用

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    
   

checkBoxButton = [[UICheckBoxButtonalloc]initWithFrame:CGRectMake(30,50,220,25)];

    checkBoxButton.delegate =self;

    NSArray *arr = [[NSArrayalloc]initWithObjects:@"check",@"uncheck",nil];

    [checkBoxButton setValue:@"test"withArrCode:arr];

    [arr release];

    [checkBoxButton setChecked:YES];

    [checkBoxButton isChecked ];

    [self.viewaddSubview:checkBoxButton];

    [checkBoxButton release]; 

    
}


-(void)checkButtonClicked{

    NSLog(@"%@",[checkBoxButtongetCode]);

}


發佈了24 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章