iphone中自定义单选按钮的默认选择

时间:2013-06-12 08:59:42

标签: iphone cocoa-touch radio-button

我想在我的应用中实现单选按钮 我使用https://www.cocoacontrols.com/controls/radiobutton(可可控制单选按钮)。 我希望默认情况下应始终选择其中一个。我该怎样才能实现这个目标。

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

`

import“RadioButton.h”

@interface RadioButton() - (无效)defaultInit; - (无效)otherButtonSelected:(ID)发送者; - (无效)handleButtonTap:(ID)发送者; @end

@implementation RadioButton

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

pragma mark - Observer

+(void)addObserverForGroupId:(NSString *)groupId observer:(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 - 管理实例

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

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

}

pragma mark - 类级别处理程序

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

}

pragma mark - 对象生命周期

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

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

    [self defaultInit];
}
return  self;

}

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

pragma mark - 点击处理

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

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

pragma mark - RadioButton init

- (无效)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 个答案:

答案 0 :(得分:2)

最初将所选图像设置为您的任何按钮。

[yourBtn setImage:yourImage forState:UIControlStateNormal]; 

根据您的代码,只需将handleButtonTap方法放入RadioButton.h

-(void)handleButtonTap:(id)sender;  

并在RadioButtonViewController.m 访问您要选择的按钮

它用于第二个按钮(即RadioButton * rb2)

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

答案 1 :(得分: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" );
    }