views:

114

answers:

4

I know how to make all these uiimageviews disappear and come back again, but I don't know how to write this shorter. They are all assigned unique tags 1-35. I want to check if ALL of them are hidden and then perform an action.

if(test.hidden==YES 
       && test2.hidden==YES
       && test3.hidden==YES 
       && test4.hidden==YES 
       && test5.hidden==YES 
       && test6.hidden==YES 
       && test7.hidden==YES 
       && test8.hidden==YES 
       && test9.hidden==YES 
       && test10.hidden==YES 
       && test11.hidden==YES 
       && test12.hidden==YES 
       && test13.hidden==YES 
       && test14.hidden==YES 
       && test15.hidden==YES 
       && test16.hidden==YES 
       && test17.hidden==YES 
       && test18.hidden==YES 
       && test19.hidden==YES 
       && test20.hidden==YES 
       && test21.hidden==YES 
       && test22.hidden==YES 
       && test23.hidden==YES 
       && test24.hidden==YES 
       && test25.hidden==YES 
       && test26.hidden==YES 
       && test27.hidden==YES 
       && test28.hidden==YES 
       && test29.hidden==YES 
       && test30.hidden==YES 
       && test31.hidden==YES 
       && test32.hidden==YES 
       && test33.hidden==YES 
       && test34.hidden==YES 
       && test35.hidden==YES){
do something
}
+5  A: 

How about keeping an array of tests instead?

Then you can simply do something like:

int testresult = 0;
for(i = 0; i < 35; i++){
   if(test[i].hidden != YES){
       testresult = 1;
       break;
   }
}

if(testresult == 0){
   // all tests = yes
}

Note this uses C arrays rather than an NSArray. Principally you should switch from having a large collection of similarly named variables to an array of these things -- in ObjC it's advisable (as per the comments on this answer) to use an NSArray.

Mark E
Why would he use a C array when he's programing in ObjC?
Colin Barrett
@Colin, are C arrays not allowed in ObjC?
Mark E
They are allowed, but NSArrays take care of the cleaning up of the objects they hold when they are released.
Eld
Fair enough, my suggestion is primarily to change the storage format, I'm not advocating for C arrays and the solution here is one of concept rather than something ObjC specific.
Mark E
+1  A: 

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/KeyValueCoding/Concepts/BasicPrinciples.html#//apple_ref/doc/uid/20002170-BAJEAIEE

This may not be exact but something like:

BOOL retVal = test.hidden ;
for (int i = 2; i < 36 && retVal; ++i ) {
  id x = [self valueForKey:[NSString stringFromFormat:@"test%d", i]];
  retVal = retVal && x.hidden ;
}
if ( retVal ) {
  // do something
}

Though I think storing the test objects in an NSArray would be better.

Eld
+6  A: 

Assuming you meant tag as in the -tag method on UIView, and these views are all in containerView

BOOL allHidden = YES;
for (int i = 1; allHidden == YES && i <= 35; i++) {
    allHidden = [[containerView viewWithTag:i] isHidden];
}
if (allHidden) {
   do_something();
}
Colin Barrett
+1  A: 

Are they all contained in the same superview? If so, try this to take advantage of the fact that they're already in an array:

@implementation UIView (testForHiddenSubviews)

- (BOOL) hasHiddenSubviews
  {
  for (UIView *view in self.subviews)
   if (view.hidden) 
    return YES;
  return NO;
  }

- (BOOL) allSubviewsHidden
  {
  for (UIView *view in self.subviews)
    if (!view.hidden) 
      return NO;
  return YES;
  }

@end
NSResponder