#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