2013-06-12 3 views
0

Je veux mettre en œuvre des boutons radio dans mon application J'ai utilisé https://www.cocoacontrols.com/controls/radiobutton (cacao contrôles Radio Buttons). Je veux que l'un d'eux soit toujours sélectionné par défaut. comment puis-je réaliser cela?sélection par défaut sur le bouton radio personnalisé dans l'iphone

enter image description here

Mon code de bouton radio est la suivante:

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 () - (vide) par défaut Init; - (void) otherButtonSélectionné: (id) expéditeur; - (void) handleButtonTap: (id) expéditeur; @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;

marque pragma - Observer

+ (void) addObserverForGroupId: (NSString *) observateur groupId: (! Rb_observers) observateur (id) { si { rb_observers = [[NSMutableDictionary alloc] init]; }

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

}

marque pragma - Gérer les instances

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

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

}

marque pragma - niveau de classe gestionnaire

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

}

marque pragma - Cycle de vie de l'objet

- (id) initWithGroupId: (NSString *) index d'ID de groupe: (NSUInteger) index { self = [super init];

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

    [self defaultInit]; 
} 
return self; 

}

  • (void) {dealloc // [_ groupId release]; // [_button release]; // [super dealloc]; }

marque pragma - Tap manipulation

- (void) handleButtonTap: (id) sender { [_button setSelected: OUI]; [bouton RadioButtonSélectionné: auto]; }

- (void) otherButtonSelected: (id) sender {// Appelé lorsque l'autre instance de bouton radio sélectionné obtenu si (_button.selected) { [_button setSelected: NO];
}}

marque pragma - RadioButton à init

- (void) defaultInit {// vue de récipient configuration 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 `

Répondre

2

Initialement fixé image sélectionnée à l'un de vos boutons.

[yourBtn setImage:yourImage forState:UIControlStateNormal]; 

Et selon votre code vient de mettre la méthode handleButtonTap dans RadioButton.h

-(void)handleButtonTap:(id)sender; 

et RadioButtonViewController.m accès sur le bouton que vous souhaitez sélectionner

Son pour le deuxième bouton (à savoirRadioButton * RB2)

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

Il fonctionne merci ..... –

+0

votre ami de bienvenue :) vous pouvez voter jusqu'à :) – Rajneesh071

+0

@ Rajneesh071: J'ai également utilisé ce code dans mon app, mais il plante et libère l'objet de UIViewController. Pourriez-vous s'il vous plaît m'aider avec cela? – Piyush

0

Essayez ceci:

pour la sélection par défaut

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

et quand vous allez à la vue suivante juste vérifier l'état ....

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"); 
    } 
Questions connexes