【发布时间】:2013-06-08 07:55:14
【问题描述】:
我想在我的应用中实现单选按钮 我使用了https://www.cocoacontrols.com/controls/radiobutton(可可控制单选按钮)。 我希望默认情况下始终选择其中之一。我怎样才能做到这一点。
我的单选按钮代码如下:
RadioButton *radioButtonForDay = [[RadioButton alloc] initWithGroupId:@"first group" index:0 ];
RadioButton *radioButtonForWeek = [[RadioButton alloc] initWithGroupId:@"first group" index:1 ];
RadioButton *radioButtonForMonth = [[RadioButton alloc] initWithGroupId:@"first group" index:2 ];
radioButtonForDay.frame = CGRectMake(40,65,22,28);
radioButtonForWeek.frame = CGRectMake(130,65,22,28);
radioButtonForMonth.frame = CGRectMake(195,65,22,28);
[self.view addSubview:radioButtonForDay];
[self.view addSubview:radioButtonForWeek];
[self.view addSubview:radioButtonForMonth];
[RadioButton addObserverForGroupId:@"first group" observer:self];
RadioButton.m
`
导入“RadioButton.h”
@interface RadioButton() -(无效)默认初始化; -(void)otherButtonSelected:(id)sender; -(void)handleButtonTap:(id)sender; @结束
@implementation 单选按钮
@synthesize groupId=_groupId; @synthesize index=_index;
静态常量 NSUInteger kRadioButtonWidth=22; 静态常量 NSUInteger kRadioButtonHeight=22;
静态 NSMutableArray *rb_instances=nil; 静态 NSMutableDictionary *rb_observers=nil;
pragma mark - 观察者
+(void)addObserverForGroupId:(NSString*)groupId 观察者:(id)observer{ 如果(!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 - 管理实例
+(void)registerInstance:(RadioButton*)radioButton{ 如果(!rb_instances){ rb_instances = [[NSMutableArray alloc] init]; }
[rb_instances addObject:radioButton];
// Make it weak reference
//[radioButton release];
}
pragma mark - 类级处理程序
+(void)buttonSelected:(RadioButton*)radioButton{
// Notify observers
if (rb_observers) {
id observer= [rb_observers objectForKey:radioButton.groupId];
if(observer && [observer respondsToSelector:@selector(radioButtonSelectedAtIndex:inGroup:)]){
[observer radioButtonSelectedAtIndex:radioButton.index inGroup:radioButton.groupId];
}
}
// Unselect the other radio buttons
if (rb_instances) {
for (int i = 0; i < [rb_instances count]; i++) {
RadioButton *button = [rb_instances objectAtIndex:i];
if (![button isEqual:radioButton] && [button.groupId isEqualToString:radioButton.groupId]) {
[button otherButtonSelected:radioButton];
}
}
}
}
pragma mark - 对象生命周期
-(id)initWithGroupId:(NSString*)groupId index:(NSUInteger)index { self = [超级初始化];
if (self) {
_groupId = groupId;
_index = index;
// _selected = selected;
[self defaultInit];
}
return self;
}
- (无效)dealloc { //[_groupId 释放]; // [_按钮释放]; // [超级释放]; }
pragma mark - 点击处理
-(void)handleButtonTap:(id)sender{ [_button setSelected:YES]; [RadioButton buttonSelected:self]; }
-(void)otherButtonSelected:(id)sender{
// 当其他单选按钮实例被选中时调用
如果(_button.selected){
[_button setSelected:NO];
}
}
pragma mark - RadioButton 初始化
-(void)defaultInit{ // 设置容器视图 self.frame = CGRectMake(0, 0, kRadioButtonWidth, kRadioButtonHeight);
// Customize UIButton
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0,kRadioButtonWidth, kRadioButtonHeight);
_button.adjustsImageWhenHighlighted = NO;
[_button setImage:[UIImage imageNamed:@"RadioButton-Unselected"] forState:UIControlStateNormal];
[_button setImage:[UIImage imageNamed:@"RadioButton-Selected"] forState:UIControlStateSelected];
[_button addTarget:self action:@selector(handleButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
[RadioButton registerInstance:self];
}
@结束 `
【问题讨论】:
标签: iphone cocoa-touch radio-button