0

https://www.cocoacontrols.com/controls/radiobutton (ココア コントロール ラジオ ボタン)を使用したアプリにラジオ ボタンを実装したい。デフォルトで常にそのうちの1つを選択する必要があります。どうすればこれを達成できますか。

ここに画像の説明を入力

私のラジオボタンのコードは次のとおりです。

  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() -(void)defaultInit; -(void)otherButtonSelected:(id)送信者; -(void)handleButtonTap:(id)送信者; @終わり

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

プラグマ マーク - オブザーバー

+(void)addObserverForGroupId:(NSString*)groupId オブザーバー:(id)オブザーバー{ 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];
}

}

プラグマ マーク - インスタンスの管理

+(void)registerInstance:(RadioButton*)radioButton{ if(!rb_instances){ rb_instances = [[NSMutableArray alloc] init]; }

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

}

プラグマ マーク - クラス レベル ハンドラ

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

}

プラグマ マーク - オブジェクトのライフサイクル

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

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

    [self defaultInit];
}
return  self;

}

  • (void)dealloc { //[_groupId リリース]; // [_ボタンを離す]; // [スーパーdealloc]; }

pragma mark - タップ処理

-(void)handleButtonTap:(id)sender{ [_button setSelected:YES]; [RadioButton buttonSelected:self]; }

-(void)otherButtonSelected:(id)sender{ // 他のラジオ ボタン インスタンスが選択されたときに呼び出されます if(_button.selected){ [_button setSelected:NO];
} }

プラグマ マーク - 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];

}

@end `

4

2 に答える 2

2

最初に、選択した画像を任意のボタンに設定します。

[yourBtn setImage:yourImage forState:UIControlStateNormal]; 

handleButtonTapそして、あなたのコードによれば、メソッドを入れるだけですRadioButton.h

-(void)handleButtonTap:(id)sender;  

そして、RadioButtonViewController.m 選択したいボタンにアクセスします

2 番目のボタン (つまり、RadioButton *rb2) 用です。

for (id subView in [rb2 subviews]) {
    if ([subView isKindOfClass:[UIButton class]]) {
        [rb2 handleButtonTap:subView];
    }
}
于 2013-06-12T09:03:09.840 に答える
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" );
    }
于 2013-06-12T09:24:39.537 に答える