IOS 单选按钮

本文介绍如何在iOS开发中创建自定义的单选按钮,通过创建名为RadioButton的UIView子类,并使用选中和取消状态的图片资源来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值