checkView

#import <Foundation/Foundation.h>


@interface OptionCheckView : UIView {
    NSMutableArray *buttonArray;
    id _delegate;
}
@property (nonatomic,retain) NSMutableArray *buttonArray;
@property (nonatomic,assign) id delegate;

-(OptionCheckView *)initWithStartPoint:(CGPoint)Point;
-(OptionCheckView *)initWithTitle:(NSString *)title delegate:(id)delegate,...;
@end

@protocol OptionCheckDelegate
-(void)OptionCheckView:(NSNumber *)which;
@end


//

//  OptionCheckView.m
//  CheckBox
//
//  Created by wgd on 12-4-9.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "OptionCheckView.h"


@implementation OptionCheckView
@synthesize buttonArray,delegate = _delegate;


-(OptionCheckView *)initWithStartPoint:(CGPoint)Point
{
    self = [self initWithFrame:CGRectMake(Point.x, Point.y, 0, 0)];
    return self;
}

 

-(OptionCheckView *)initWithTitle:(NSString *)title delegate:(id)delegate,...
{    
    self.delegate = delegate;
    //self.backgroundColor = [UIColor greenColor];
    
    CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(768, 1024)];
    
    UILabel *firstLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, titleSize.width*1.4, titleSize.height+4)];

    firstLab.backgroundColor = [UIColor clearColor];
    firstLab.text = title;
    [self addSubview:firstLab];
    [firstLab release];
    
    NSString *option;
    va_list optionList;
    self.buttonArray = [[NSMutableArray alloc] init];
    [buttonArray release];
 
        va_start(optionList,delegate);
        int num = 0;
        while (option=va_arg(optionList,id)) {
            
            UIButton *checkBtn = [[UIButton alloc] initWithFrame:
                                  CGRectMake(titleSize.width*1.2, (titleSize.height+11)*num+1,18,18)];
            
            [checkBtn setBackgroundImage:[UIImage imageNamed:@"checkbox_circle.png"] forState:UIControlStateNormal];
            [checkBtn setImage:[UIImage imageNamed:@"checkbox_circle_focus.png"] forState:UIControlStateSelected];
            [checkBtn addTarget:self action:@selector(checkOption:) forControlEvents:UIControlEventTouchUpInside];
            checkBtn.backgroundColor = [UIColor clearColor];
            checkBtn.tag = num+1;
            [self addSubview:checkBtn];
            [checkBtn release];
            [buttonArray addObject:checkBtn];
            static CGSize maxSize;
              CGSize optionSize = [option sizeWithFont:[UIFont systemFontOfSize:12]
                                   constrainedToSize:CGSizeMake(768, 1024)];
            if(optionSize.width >maxSize.width)
            {
                 maxSize = optionSize;
            }
            
            UILabel *optionLab = [[UILabel alloc] initWithFrame:
                                  CGRectMake(titleSize.width*1.2+25,(titleSize.height+11 )*num+1, optionSize.width*1.6, optionSize.height)];
            optionLab.text = option;
            optionLab.backgroundColor = [UIColor clearColor];
            [self addSubview:optionLab];
            [optionLab release];
            num++;
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, optionSize.width*1.6+25 + maxSize.width*1.6, (titleSize.height+10)*num);
            
        }
    
    return self;
    
}


-(void)checkOption:(id)sender
{
    UIButton *selectBtn = (UIButton *)sender;
    UIButton *checkBtn;
    for (id btn in  buttonArray)
    {
        if([btn isEqual:sender])
        {    
            if(selectBtn.selected)
            {
                selectBtn.selected = NO;
            }
            else
            {
                selectBtn.selected = YES;
            }
            checkBtn = selectBtn;
        }
        else
        {
            ((UIButton *)btn).selected = NO;
        }

    }
    if((nil !=_delegate)&&[_delegate respondsToSelector:@selector(OptionCheckView:)])
    {
        [_delegate performSelector:@selector(OptionCheckView:) withObject:[NSNumber numberWithInt:checkBtn.tag]];
    }
    
}

-(void)dealloc
{
    [buttonArray release];
    [super dealloc];
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值