iOS自定义 多选按钮 multiradio

本文详细介绍了如何在iOS中创建自定义的多选按钮(MultiRadioButton),包括按钮的初始化、触控处理、组选择逻辑以及如何与其他组件交互。通过MultiRadioButton类的实现,可以方便地管理同一组内的多个选项,并通过代理方法通知选中状态的变化。
摘要由CSDN通过智能技术生成

1.MultiRadioButton.h

#import <UIKit/UIKit.h>

@protocol MutltiRadioButtonDelegate <NSObject>

-(void)MutltiradioButtonSelectedAtIndex:(NSUInteger)index inGroup:(NSString*)groupId;

-(void)MutltiradioButtonTouchDownAtIndex:(NSUInteger)index inGroup:(NSString*)groupId;


@end


@interface MultiRadioButton : UIView {

    NSString *_groupId;

    NSUInteger _index;

    UIButton *_button;

}

@property(nonatomic,retain)NSString *groupId;

@property(nonatomic,assign)NSUInteger index;


-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index;

+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer;


-(void)handleButton1;

-(void)handleButton;

+(void)buttonTouchDown:(MultiRadioButton*)radioButton;

// 可以设置默认选中项

- (void) setChecked:(BOOL)isChecked;

@end

2.MultiRadioButton.m

#import "MultiRadioButton.h"

#define kRadioButtonNotification @"kRadioButtonNotification"


@interface MultiRadioButton()

-(void)defaultInit;

-(void)otherButtonSelected:(id)sender;

-(void)handleButtonTap:(id)sender;

@end


@implementation MultiRadioButton


@synthesize groupId=_groupId;

@synthesize index=_index;


static const NSUInteger kRadioButtonWidth=300;

static const NSUInteger kRadioButtonHeight=80;


static NSMutableArray *rb_instances=nil;

static NSMutableDictionary *rb_instancesDic=nil// 识别不同的组

static NSMutableDictionary *rb_observers=nil;

#pragma mark - Observer


+(void)addObserverForGroupId:(NSString*)groupId observer:(id)observer{

    

    if(!rb_observers){

        rb_observers = [[NSMutableDictionary alloc] init];

    }

    

    if ([groupId length] > 0 && observer) {

        [rb_observers setObject:observer forKey:groupId];

        // Make it weak reference

        //[observer release];

    }

    

}


#pragma mark - Manage Instances


+(void)registerInstance:(MultiRadioButton*)radioButton withGroupID:(NSString *)aGroupID{

    

    if(!rb_instancesDic){

        rb_instancesDic = [[NSMutableDictionary alloc] initWithCapacity:16];

    }

    

    if ([rb_instancesDic objectForKey:aGroupID]) {

        [[rb_instancesDic objectForKey:aGroupID] addObject:radioButton];

        [rb_instancesDic setObject:[rb_instancesDic objectForKey:aGroupID] forKey:aGroupID];

        //[radioButton release];

        

    }else {

        NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:16];

        [arr addObject:radioButton];

        // [radioButton release];

        [rb_instancesDic setObject:arr forKey:aGroupID];

        [arr release];

    }

}


#pragma mark - Class level handler


+(void)buttonSelected:(MultiRadioButton*)radioButton

{

    

    

    // Notify observers

    if (rb_observers) {

        id observer= [rb_observers objectForKey:radioButton.groupId];

        

        if(observer && [observer respondsToSelector:@selector(MutltiradioButtonSelectedAtIndex:inGroup:)]){

            [observer MutltiradioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId];

        }

    }

    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值