1、首先创建一个工程,命名为RadioButton,
然后创建RadioButton类,继承自UIView,同时添加资源文件,选中、取消的图片,添加完成以后目录如下:
RadioButton类头文件代码如下:
#import <UIKit/UIKit.h>
@protocol RadioButtonDelegate;
@interface RadioButton : UIView
@property(nonatomic,retain)NSString *text;//文本
@property(nonatomic,assign)BOOL checked;
@property(nonatomic,copy)NSString *groupId;//组
@property(nonatomic,retain)id<RadioButtonDelegate> delegate;
-(id)initWithText:(NSString *)text groupId:(NSString *)groupid frame:(CGRect)frame;
@end
@protocol RadioButtonDelegate<NSObject>//协议
-(void)onChangeDelegate:(RadioButton *)radio isCheck:(BOOL)isCheck;
@end
实现代码如下:
#import "RadioButton.h"
@interface RadioButton()
@property(nonatomic,retain)UIImage *onImage;
@property(nonatomic,retain)UIImage *offImage;
@end
static NSMutableDictionary *_mutableDic=nil;
@implementation RadioButton
-(void)dealloc{
[self remove_group];
[_text release];
[_onImage release];
[_offImage release];
[_delegate release];
[_mutableDic release];
[_groupId release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)setChecked:(BOOL)checked{
if (_checked == checked) {
return;
}
_checked = checked;
self.checked= checked;
<pre name="code" class="objc">if(self.checked){
[self cancelOtherChecked];
}
[self setNeedsDisplay];
if (self.selected && _delegate && [_delegate respondsToSelector:@selector(onChangeDelegate:groupId:)]) {
[_delegate onChangeDelegate:self groupId:_groupId];
}
}-(id)initWithText:(NSString *)text groupId:(NSString *)groupid frame:(CGRect)frame{ self=[super initWithFrame:frame]; if(self){ _text=text; self.backgroundColor=[UIColor clearColor]; self.onImage=[UIImage imageNamed:@"radio_selected.png"]; self.offImage=[UIImage imageNamed:@"radio_unselected.png"]; _groupId=[groupid copy]; [self add_group]; } return self;}//相同的添加到一个组中-(void)add_group{ if(!_mutableDic){ _mutableDic=[[NSMutableDictionary dictionary] retain]; } NSMutableArray *_gradios=[_mutableDic objectForKey:_groupId]; if(!_gradios){ _gradios=[NSMutableArray array]; } [_gradios addObject:self]; [_mutableDic setObject:_gradios forKey:_groupId];}//让其他选中的取消-(void)cancelOtherChecked{ NSMutableArray *_gradios=[_mutableDic objectForKey:_groupId]; if(_gradios.count>0){ for(RadioButton *_radio in _gradios){ if(_radio.checked&&![_radio isEqual:self]){ _radio.checked=NO; } } }}//移除同一个组中的元素-(void)remove_group{ if(_mutableDic){ NSMutableArray *_gradios=[_mutableDic objectForKey:_groupId]; if(_gradios){ [_gradios removeObject:self]; if(_gradios.count==0){ [_mutableDic removeObjectForKey:_groupId]; } } }}//将图片,文字绘制到UIView上面-(void)drawRect:(CGRect)rect{ UIImage *image=self.checked?self.onImage:self.offImage; [image drawAtPoint:CGPointMake(5, 8)]; UIFont *font=[UIFont systemFontOfSize:16.0f]; [self.text drawAtPoint:CGPointMake(25, 8) withFont:font]; }//点击事件-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ self.checked=!self.checked;}@end
调用代码如下:首先引入RadioButton,
RadioButton *rb=[[RadioButton alloc] initWithText:@"男" groupId:@"1" frame:CGRectMake(10, 10, 80, 30)];
rb.delegate=self;
[rb setChecked:YES];
[self.view addSubview:rb];
[rb release];
RadioButton *female=[[RadioButton alloc] initWithText:@"女" groupId:@"1" frame:CGRectMake(100, 10, 180, 30)];
female.delegate=self;
[self.view addSubview:female];
[female release];
RadioButton *rb2=[[RadioButton alloc] initWithText:@"移动" groupId:@"2" frame:CGRectMake(10, 70, 80, 30)];
rb2.delegate=self;
[rb2 setChecked:YES];
[self.view addSubview:rb2];
[rb2 release];
RadioButton *female2=[[RadioButton alloc] initWithText:@"电信" groupId:@"2" frame:CGRectMake(100, 70, 180, 30)];
female2.delegate=self;
[self.view addSubview:female2];
[female2 release];