Vork
Vork

Reputation: 744

Select/Deselect for dynamically created UIButtons

I will explain the Scenario: I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.

for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
  self.graphSubDataButton = [[UIButton alloc] init];


    self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
    [self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];

    [self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];

    self.graphSubDataButton.tag = btnsCount+1;
    [self.graphSubDataButton addTarget:self action:@selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
    buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;

if (self.graphSubDataButton.tag==1)
    {

        [self.graphSubDataButton.layer setBorderWidth:1.2f];
        [self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
        [self.graphSubDataButton.layer setCornerRadius:8.0f];
    }
}   


-(void) enableRespectiveButton2:(UIButton *) sender
{
}   

Upvotes: 0

Views: 467

Answers (4)

Bharath Vankireddy
Bharath Vankireddy

Reputation: 932

Create dynamic buttons with for loop as shown below.

for(int i = 0 ;i <3; i++) {
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    NSString * title = [NSString stringWithFormat:@"Button %d",i];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"BorderImg"] forState:UIControlStateSelected];
    [btn setBackgroundImage:[UIImage imageNamed:@"UnBordered"] forState:UIControlStateNormal];
    if(i == 0) [btn setSelected:YES];
    else [btn setSelected:NO];
    [btn setTag:i];
    [btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}

Have 2 images

  1. Highlighted border (BorderImg)
  2. Normal border (UnBordered)

Set those images to button while creating in for loop

- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
    [btn setSelected:YES];
}

Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

Upvotes: 1

VRAwesome
VRAwesome

Reputation: 4803

Create dynamic buttons like :

int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor blueColor];
    btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
    btn.tag = btnsCount+1;
    [btn addTarget:self action:@selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
    buttonsOffset = buttonsOffset+30 + btn.frame.size.width;

    if (btn.tag==1)
    {
        [btn.layer setBorderWidth:1.2f];
        [btn.layer setBorderColor:[UIColor greenColor].CGColor];
        [btn.layer setCornerRadius:8.0f];
        self.graphSubDataButton = btn;
    }
    [self.view addSubview:btn];
}

And click event of button is as following

- (void)enableRespectiveButton2:(UIButton *)sender
{
    sender.selected = TRUE;
    [sender.layer setBorderWidth:1.2f];
    [sender.layer setBorderColor:[UIColor greenColor].CGColor];
    [sender.layer setCornerRadius:8.0f];

    self.graphSubDataButton.selected = FALSE;
    [self.graphSubDataButton.layer setBorderWidth:0.0f];
    [self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
    [self.graphSubDataButton.layer setCornerRadius:0.0f];
    self.graphSubDataButton = sender;
}

Upvotes: 1

ares777
ares777

Reputation: 3628

Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons

    for (UIView *subview in [uiv_ButtonsView subviews]) { 
    if([subview isKindOfClass:[UIButton class]]) {
        subview.layer  setBorderWidth:0.0f;
        }
     } 

then use:

    UIButton *button=(UIButton *)sender;
    [button.layer setBorderWidth:1.2f];
    [button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
    [button.layer setCornerRadius:8.0f];

Upvotes: 1

Hemang
Hemang

Reputation: 27072

I have changed your code structure little to make it work properly, you can comment if you don't understand something.

NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
   UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
   graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
   [graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
   [graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
   graphSubDataButton.tag = btnsCount+1;
   [graphSubDataButton addTarget:self action:@selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
   buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
   //add your button somewhere
   //e.g.
   //[self.view addSubview:graphSubDataButton];
   if (graphSubDataButton.tag == defaultSelectedTag) {
       [self setBorderForButton:graphSubDataButton];
   }
}

- (void) setBorderForButton:(UIButton *)sender {
    [sender.layer setBorderWidth:1.2f];
    [sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
    [sender.layer setCornerRadius:8.0f];
}

- (void) enableRespectiveButton2:(UIButton *) sender {
    for(UIButton *buttons in [sender.superview subviews]) {
        if([buttons isKindOfClass:[UIButton class]]) {
            if(![buttons isEqual:sender]) {
                //add logic to, remove borders
                //buttons.layer.borderWidth = 0.0f;
            }
        }
    }
    [self setBorderForButton:sender];
}

Upvotes: 1

Related Questions