1

I have 5 buttons in my xib, Button 1 to 4 is mapped to

-(IBAction)btn1to4:(UIButton *)sender; 

Button 5 is mapped to

-(IBAction)btnFive:(id)sender;

Initially all the 4 buttons are hidden and only button 5 is visible, What I need is when I click on Button 5 all the 4 buttons should appear and when I click again on Button 5 they should disappear. For individual button I can write code in Button 5 as button1.hidden=NO, button2.hidden=NO and soon. But my buttons 1 to 4 are mapped to single btn1to4 method. How should I code in my btnFive method to hide/Unhide all 4 buttons at once?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • When you say that Button 1 to 4 are mapped to `-(IBAction)btn1to4:(UIButton *)sender`, that does not matter much: it's just an event handler. You can create individual `IBOutlet`s for your buttons, or create an `IBOutletCollection` - a preferred way to manage groups of elements in Cocoa. – Sergey Kalinichenko Aug 16 '13 at 10:48

4 Answers4

2

Add buttons 1 through 4 to an IBOutletCollection in the interface builder. Add a property

@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons1_4;

and drag buttons 1..4 there. Here is an answer explaining how to do it step-by-step.

Now you can operate all the buttons in that collection using a loop, rather than referencing them individually:

-(void)flipButtonsVisibility:(UIButton*)sender {
    for (UIButton *btn in buttons1_4) {
        btn.hidden = !btn.hidden;
    }
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • In addition to this, if I had to change the title of the 5th button accordingly like, when I click on show its title should change to hide and vice versa. How should I proceed? if(btn.hidden) sender.titleLabel.text=@"Show"; doesnt seem to work. – Francis F Aug 16 '13 at 11:11
  • @Gamerlegend Check out [this question](http://stackoverflow.com/a/16358700/335858), it explains how to change the title. – Sergey Kalinichenko Aug 16 '13 at 11:12
1

Give tag of your button5 such like

button5.tag = 101;

In you button5's IBAction change id to UIButton * in parameter, such like

-(IBAction)btnFive:(UIButton *)sender

And Write following code

-(IBAction)btnFive:(UIButton *)sender
{
    if(sender.tag == 101)
    {
      self.btn1.hidden = YES;
      self.btn2.hidden = YES;
      self.btn3.hidden = YES;
      self.btn4.hidden = YES;
      sender.tag = 102;
    }
    else
    {
      self.btn1.hidden = NO;
      self.btn2.hidden = NO;
      self.btn3.hidden = NO;
      self.btn4.hidden = NO;

      sender.tag = 101;
    }
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • There is no individual, btn1, btn2, btn3, btn4, jst a single method btn1to4. So self.btn1to4 will not work. Any other alternatives?? – Francis F Aug 16 '13 at 10:44
  • What do you mean **There is no individual, btn1, btn2, btn3, btn4** ?? – iPatel Aug 16 '13 at 10:48
1

in your .h file

int a;

.m

-(void)viewDidLoad
{
 a=0;
}

In your button click

-(IBAction)btnFive:(id)sender


{
    if(a==0)
    {
      button1.hidden = YES;
      button2.hidden = YES;
      button3.hidden = YES;
      button4.hidden = YES;
      a = 1;

    }
    else
    {
      button1.hidden = NO;
      button2.hidden = NO;
      button3.hidden = NO;
      button4.hidden = NO;
      a = 0;
    }
}
Bug
  • 2,576
  • 2
  • 21
  • 36
0

In your -(IBAction)btnFive:(id)sender, first check any one button (from 1-4) hidden property & do its opposite in condition. please find sample below -

-(IBAction)btnFive:(id)sender {    
if(btn4.hidden==false){
          btn1.hidden=true;
          btn2.hidden=true;
          btn3.hidden=true;
          btn5.hidden=true;
    }else{
         btn1.hidden=false;
         btn2.hidden=false;
         btn3.hidden=false;
         btn5.hidden=false;
    }
}

try to write less number of line code and try to write effective lines.

If you need more help, please let me know. And if you find this answer suitable, Please vote me. All the Best

Karam
  • 29
  • 3