0

I want to have three sets of 10 buttons for an iOS app. I want to show/hide each set (reveal one set at a time). I am planning to use tags to do this.

As it is I have a property for each button (ex: btn01, btn02, etc), so I have 30 properties. I add that to an array, and then use the following code to hide buttons with one tag or the other.

My question is, is there a more efficient way to create the array of objects, rather than setting up 30 properties (one for each button). It's fine, just wonder if there is a cleaner way, using a loop to gather each button from IB somehow. Can't think of it.

    if (tagState == 1) {
        tagState = 2;
    }else {
        tagState = 1;
    }

    for (btn in myArray) {
        NSLog(@"%@",btn);
        if (tagState == 1) {
            if (btn.tag == 1) {
                [btn setHidden:YES];
            }
            if (btn.tag == 2) {
                [btn setHidden:NO];
            }

        } else if (tagState == 2) {
            if (btn.tag == 1) {
                [btn setHidden:NO];
            }
            if (btn.tag == 2) {
                [btn setHidden:YES];
            }

        }    
    }
}
1
  • 1
    Could you add each set of buttons to a different UIView and then simply hide/show each view as needed? Commented Jul 12, 2012 at 5:02

2 Answers 2

1

I think you can use the view hierarchy very effectively here: put each group of buttons into its own UIView, connect only the three UIViews to outlets, and hide/show them. Note that this only works if you don't also need to access the individual buttons for some other reason.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is what I am going to do. Thanks
1

You can use IBOutletCollection, it is an NSArray of outlets.

To e.g. hide all outlets from a collection, you can use

[outlets setValue:@YES forKey:@"hidden"]

or

[outlets setValue:[NSNumber numberWithBool:YES] forKey:@"hidden"]

when using LLVM < 4.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.