1

I'm looking to hide a button on my i-phone app and then by clicking another button it will appear. I've managed to make the button disappear with a click but can't figure out the opposite. I'm also new to Objective-C as you can probably tell so any tips on improving my code would be helpful. Thanks!

.h :

@property(nonatomic,retain) IBOutlet UIButton* button1 ;


-(IBAction)buttonTouch:(id)sender ;

.m :

@synthesize button1=_button1;

-(BOOL)hideOutlets {    
    _button1.hidden=TRUE;
}

-(void)buttonTouch:(id)sender {
    _button1.hidden =  !_button1.hidden;
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Andrea F
  • 733
  • 2
  • 9
  • 16
  • 3
    This code is working. Note that you can't touch a `hidden` button. How are you trying to show it? – Sulthan May 25 '13 at 12:44

5 Answers5

4

Well to start from scratch, if you want to hide a button set its property hidden to YES, else if you want to make it reappear then set the property to NO.

Example:

button1.hidden=YES;
button1.hidden=NO;
hypercrypt
  • 15,389
  • 6
  • 48
  • 59
Maurice
  • 1,466
  • 1
  • 13
  • 33
  • In Objective-C you normally don't begin a local or instance variable name with an upper case letter. It is too easy to confuse with a class name. – Monolo May 25 '13 at 12:53
  • Well yes you are right, I edited it, it was just a quick example so I forgot to actually care about it... also it is a thing of the developer - declaring variables is a thing what is not specified by some authority – Maurice May 25 '13 at 12:58
  • 2
    Absolutely true - but a programming language is also a means of communication between developers, who can be separated by years and even continents. In these cases following conventions helps, especially when it doesn't otherwise pose problems in the code. – Monolo May 25 '13 at 13:11
  • sure, thats why I edited it here, but what you are doing at home in your program with your variables is something what no other developer cares about so... you were right in your first post - thats why edited it here because it's a Askingsite for Devs... nothing more to talk about right?? – Maurice May 25 '13 at 13:41
1

Your code is basically correct

-(void)buttonTouch:(id)sender {
    _button1.hidden =  !_button1.hidden;
}

This code will hide your button when it's shown and show it when it's hidden. This should be correct.

You are saying

then by clicking another button it will appear

Are you sure both buttons have the touch-up-inside event properly connected to this action? I guess your problem will be that the buttonTouch: is not called when you touch the other button.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
0
 @synthesize button1=_button1;
    -(BOOL)hideOutlets {

    _button1.hidden=TRUE;

}

-(void)buttonTouch:(id)sender {

        _button1.hidden = FALSE; //Or "NO" or "0", it's a boolean 
}

In addition, its weird setting a button hidden with a BOOL. If you want to have them hidden on load, go put _button1.hidden = YES; if you want it to hide it only when you have it visible, use

-(void)buttonTouch:(id)sender {
if(_button1.hidden == YES) 
    {
        _button1.hidden = NO; 
    }
else { _button1.hidden = YES; }
 }
Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92
  • Comparing with `YES` is a terrible code and sometimes it won't do what you expect, use `if (_button1.hidden)` instead. – Sulthan May 25 '13 at 12:41
  • 1
    You probably never worked with someone else's code, or with C or obj-C++. `BOOL valid = 20; if (valid) { do something } else if (valid == YES) {}`. However, comparing a `BOOL` with `YES` or `NO` is unneccessary and makes reading more difficult. Compare "if it's hidden" and "if hidden is yes" – Sulthan May 25 '13 at 12:53
  • @Sulthan Probably, but for helping a newcomer understand code I would assume =YES is easier to get the hang of. – Oscar Apeland May 25 '13 at 12:57
  • There is also the .isHidden property that can be accessed which makes the code more readable and works the same way. – Bill Burgess May 25 '13 at 14:00
0

I'll try to answer the question correctly as I understand it.

2 buttons, button1 and button2. Pressing button1 hides itself and shows button2. Pressing button2 hides itself and shows button1 again.

-(IBAction)button1Pressed:(id)sender {
    // button1 can only be pressed when not hidden, so we can dispense with checks for hidden
    [button1 setHidden:YES];
    [button2 setHidden:NO]; // assuming this button was hidden at startup
}

-(IBAction)button2Pressed:(id)sender {
    // button2 can only be pressed when not hidden, so no need to check for hidden
    [button2 setHidden:YES];
    [button1 setHidden:NO];
}

This should allow you to flip back and forth between buttons having them hide/show opposite of each other.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
-2

Two obvious problems with the code presented.

1) Cocoa uses YES and NO for boolean values not TRUE and FALSE.

2) You've declared a property, so you should use it in preference to the synthesised instance variable.

3) You're button touch method should return IBAction in the implementation as well as the interface.

Don't know if that'll fix your problem, but it's the first step to fix up your code.

@synthesize button1=_button1;

-(BOOL)hideOutlets {

    self.button1.hidden=YES;

}

-(IBAction)buttonTouch:(id)sender {

    self.button1.hidden =  !self.button1.hidden;
}
Steve Waddicor
  • 2,197
  • 15
  • 20