2013-06-12 118 views
0

我想在我的应用程序中实现单选按钮 我使用了https://www.cocoacontrols.com/controls/radiobutton(可可控件单选按钮)。 我希望他们中的一个应该总是被默认选中。我怎么能实现这一点。iPhone上的自定义单选按钮的默认选择

enter image description here

我的单选按钮的代码如下:

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单选按钮( ) - (void)默认值在里面; - (void)otherButtonSelected:(id)sender; - (void)handleButtonTap:(id)sender; @end

@implementation单选按钮

@synthesize的groupId = _groupId; @synthesize index = _index;

static const NSUInteger kRadioButtonWidth = 22; static const NSUInteger kRadioButtonHeight = 22;

static NSMutableArray * rb_instances = nil; static NSMutableDictionary * rb_observers = nil;

编译标记 - 观察员

+(无效)addObserverForGroupId:(的NSString *)的groupId观察者:(!rb_observers)(id)的观察者{ 如果{ rb_observers = [[ALLOC的NSMutableDictionary] INIT]; }

if ([groupId length] > 0 && observer) { 
    [rb_observers setObject:observer forKey:groupId]; 
    // Make it weak reference 
    //[observer release]; 
} 

}

编译马克 - 管理实例

+(无效)registerInstance:(单选*)单选按钮{ 如果(!rb_instances){ rb_instances = [[NSMutableArray alloc] init]; }

[rb_instances addObject:radioButton]; 
// Make it weak reference 
//[radioButton release]; 

}

编译标记 - 类级处理程序

+(无效)buttonSelected:(单选按钮*)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]; 
     } 
    } 
} 

}

编译指示标记 - 对象生命周期

- (id)initWithGroupId:(NSString *)groupId index:(NSUInteger)index { self = [super init];

if (self) { 
    _groupId = groupId; 
    _index = index; 
    // _selected = selected; 

    [self defaultInit]; 
} 
return self; 

}

  • (无效)的dealloc { // [_的groupId释放]; // [_button release]; // [super dealloc]; }

编译标记 - 点击处理

- (无效)handleButtonTap:(ID)发送方{ [_button的setSelected:YES]; [RadioButton buttonSelected:self]; }

- (无效)otherButtonSelected:(ID)发送方{ //当其他单选按钮实例得到了选择 调用如果(_button.selected){ [_button的setSelected:NO];
} }

编译标记 - 单选按钮初始化

- (无效)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]; 

}

@end `

回答

2

初步确定所选图像到任何按钮。

[yourBtn setImage:yourImage forState:UIControlStateNormal]; 

,并根据您的代码只是把handleButtonTap方法RadioButton.h

-(void)handleButtonTap:(id)sender; 

RadioButtonViewController.m 访问要选择

其为第二个按钮的按钮(即单选按钮* RB2)

for (id subView in [rb2 subviews]) { 
    if ([subView isKindOfClass:[UIButton class]]) { 
     [rb2 handleButtonTap:subView]; 
    } 
} 
+0

它的工作原理谢谢..... –

+0

你的欢迎哥们:)你可以投票:) – Rajneesh071

+0

@ Rajneesh071:我也用过这段代码在我的应用程序,但它正在崩溃和释放UIViewController的对象。你能帮我解决这个问题吗? – Piyush

0

试试这个:

为默认选择

 [btnR setImage:[UIImage imageNamed:@"btnCheck.png"] forState:UIControlStateNormal]; 
     [btnG setImage:[UIImage imageNamed:@"btnUnCheck.png"] forState:UIControlStateNormal]; 
     [btnB setImage:[UIImage imageNamed:@"btnUnCheck.png"] forState:UIControlStateNormal]; 

,当你去下一个视图只检查条件....

UIImage* selectedImg=[UIImage imageNamed:@"btnCheck.png"]; //btnCheck.png your image name 
    if (btnR.imageView.image == selectedImg || btnG.imageView.image == selectedImg || btnB.imageView.image == selectedImg) 
    { 
      //One of them is selected 
    } 
    else { 
     NSLog(@"Please Select At least One Color"); 
    }