6

New to Objective-C and iOS development, would love a hand here!

I have written up some code such that

IBOutletCollection(UILabel) NSArray *allLabels;

In IB I have linked up all my labels in my view to this collection, where I want to hide them for a certain condition. However, I am not sure how to do so. Obviously to hide a single label I'd use

labelX.hidden = YES;

however it is not ideal for me to do this without a collection, as I have many labels to hide.

Thanks for your tips in advance!

Ben T
  • 141
  • 1
  • 10

3 Answers3

10

try this...

[allLabels setValue:@(YES) forKey:@"hidden"];
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • @Vladimir which will be faster.. ? the one suggested by mit3z or by me.. ? just out of curiosity. – Ankit Srivastava May 28 '12 at 08:06
  • Made some quick tests, so no guarantee that they are adequate. Your method (tested on iPod touch with array of 50 labels) runs 0.0022 - 0.0029 sec, mit3z's method runs: 0.0015-0.002s so is a bit faster – Vladimir May 28 '12 at 08:26
  • @Vladimir would you say blocks are generally faster than methods..? is it that why it is happening? – Ankit Srivastava May 28 '12 at 08:29
  • Works perfectly. Thanks! However, what would I use in opposition of this - ie to re-show the labels? Thank you again! – Ben T May 28 '12 at 08:31
  • @BenToscano NO instead of YES. – Ankit Srivastava May 28 '12 at 08:31
  • @AnkitSrivastava, not sure why exactly it works slower. Possible reasons - time need to boxing/unboxing bool value to NSNumber, time to resolve actual selector for key name – Vladimir May 28 '12 at 08:33
  • Ah, I originally tried that and got an error but it was something else so it worked now. Thanks again for your time! – Ben T May 28 '12 at 08:35
2

Just enumerate collection and do whatever you want with contents:

[allLabels enumerateIndexesUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
    label.hidden = YES;
}];
ksh
  • 1,894
  • 19
  • 20
0

Swift Version for Array type:

(allLabels as NSArray).setValue(NSNumber(bool: true), forKey: "hidden")
nobody
  • 10,892
  • 8
  • 45
  • 63
TejAces
  • 551
  • 5
  • 11