konyv12 konyv12 -4 years ago 125
iOS Question

How to alternate between hidden=YES and hidden=NO on a button click in iOS?

I have the hidden property set to YES on two UIViews in viewDidLoad. On a button click, they will be set to NO, therefore they will be shown. How do I make these properties switch between yes/no on every button click?

Answer Source

If the two views are, view1 and view2, you would set up the button to hook to an action such as the following:

-(IBAction)buttonClicked:(id)sender {
    view1.hidden = !view1.hidden;
    view2.hidden = !view2.hidden;

The ! is the logical NOT operator. So it will take the current value for hidden (whether it is true or false) and convert it to the opposite value - so, true if it was false, or false, if it was true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download