Hiding all UILabels in NSMutableArray
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<siz开发者_C百科eof(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 sizeof
is 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:
for (UILabel *label in labelArray) {
label.hidden = !label.hidden;
}
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 init
to nil does not create an object. Either way, you should probably be doing labelArray = [[NSMutableArray alloc] initWithObjects:label1,label2,label3,nil];
. alloc
and init
go together hand-in-glove.
精彩评论