I have various UILabels that I would like to hide using a for-loop.
@interface MyViewController : UIViewController {
NSMutableArray * labelArray;
}
@property (nonatomic, retain) IBOutlet UILabel *label1, *label2, *label3;
...
-(void)viewDidLoad {
[super viewDidLoad];
[labelArray initWithObjects:label1,label2,label3,nil];
for(int i=0; i<sizeof(labelArray); i++){
UILabel *label = [labelArray objectAtIndex:i];
label.hidden = !label.hidden;
}
}
When this is executed, the labels are not hidden. They have been “hooked up” in Interface Builder. What am I doing incorrectly? Thanks!
That is not what
sizeofis for. That’s a compiler construct that tells you how many bytes a value takes up, which has no clue how many elements are in an NSMutableArray at runtime. You want:If that doesn’t work, then your array does not contain the objects you believe it does — quite possibly, you’ve forgotten to actually create the array — simply sending
initto nil does not create an object. Either way, you should probably be doinglabelArray = [[NSMutableArray alloc] initWithObjects:label1,label2,label3,nil];.allocandinitgo together hand-in-glove.