Objective C - Hiding multiple UI elements
So 开发者_开发百科I have an application made from 2 view controllers, one controller is a main screen where all the UI elements are displayed, the other controller handles what UI elements are on the main controller.
Currently I have lots of statements like to hide / show certain elements depending on what the user wants to display.
label.hidden = TRUE;
label2.hidden = TRUE;
label3.hidden = FALSE;
Obviously the actual application is a lot larger than this and there are a lot more statements.
I was wondering if there is a better way to do this?
I was thinking a seperate UIView for each possible main screen variation. Then display a UIView depending on what elements are to be displayed. Would this be correct?
Thanks
I think you should even create multiple viewControllers.
A parent UIView as you suggest is probably the best option in terms of keeping the hierarchy obvious and editable in Interface Builder (or the Xcode 4 analogue thereof).
An alternative would be to put all the views you want to toggle into an array just once, and perform:
[arrayOfViews makeObjectsPerformSelector:@selector(setHidden:)
withObject:[NSNumber numberWithBool:YES]];
Which has the effect of hiding every single view in the array.
In my apps I tend to have some groups of views that I can move or hide/show together, in which case I collect them in a single parent view, some that move or hide/show alone, in which case I obviously treat them singly, and some that are so different from one orientation to the other that I keep alternates as you suggest.
Straying slightly from the topic, if you're doing distinct views for different orientations you might be better implementing something like:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
portraitView.alpha = 1.0f;
landscapeView.alpha = 0.0f;
}
else
{
portraitView.alpha = 0.0f;
landscapeView.alpha = 1.0f;
}
}
Which will give a sort of cross-fade between the two views as part of the rotation animation because a CoreAnimation block is automatically arranged around willAnimateRotationToInterfaceOrientation:duration: and alpha is an animatable property.
精彩评论