Reputation: 3295
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?
Upvotes: 1
Views: 1341
Reputation: 29
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
Upvotes: 0
Reputation: 2572
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;
}
}
Upvotes: 1
Reputation: 727137
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;
}
}
Upvotes: 2
Reputation: 47119
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;
}
}
Upvotes: 1