Dynamically created tags overridden

I am working on a single application in which I added 5 labels dynamically in a function. When I recall the same function, labels are redefined on previously created labels, despite the release of labels for each creation.

for(int i = 1; i < [array count]; i++) { CGRect lblframe = CGRectMake(count, ycord, width, height); UILabel *label = [[UILabel alloc] initWithFrame:lblframe]; label.backgroundColor = [UIColor blackColor]; label.font = [UIFont systemFontOfSize:17]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor colorWithRed:(188/255.f) green:(149/255.f) blue:(88/255.f) alpha:1.0];; label.text = [arr objectAtIndex:i]; count = count + xcord; [subcatScroll addSubview:label]; [label release]; } 
+4
source share
2 answers

Write the before for code below to get your requirement:

 for (id object in [subcatScroll subviews]) { [object removeFromSuperview]; } 
+5
source

I'm not sure that I am fully following, so correct me if I do not understand. Each time you call this function, you add a few new labels. Therefore, if you call this function a second time, counting "count", "ycord", "width" and "height", correspond to the values ​​that the first call had, you obviously add a second group of labels in the same place like the first ones that are now directly above each other. You do not "redefine" the old labels, you put the second group directly on top of the old ones.

Calling "release" on each label means that you decrease the value of keepCount by 1. This number is used only for memory management. This means that now you remove the shortcuts from the view in which the memory is issued.

 CGRect lblframe = CGRectMake(10.0, ycord, 200.0, 20.0); UILabel *label = [[UILabel alloc] initWithFrame:lblframe]; NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "1" since you called alloc [self.view addSubview:label]; NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "2" since adding to subview increases retaincount by one [label release]; NSLog(@"retainCount of label: %d", [label reatinCount]); // will print "1" since you released [label removeFromSuperview]; // will decrease retainCount of label to "0" and therefore free the memory 

so say you wanted to remove previously created labels from the view, you have to do it. Or keep a link to each of them, and call "removeFromSuperview" for each of them.

If in the view in which you add tags, you can simply delete each subheading that has been added to it, for example:

 // remove old labels for (UILabel *aLabel in [self.view subviews]) [aLabel removeFromSuperview]; NSArray *myArray = [NSArray arrayWithObjects:@"I", @"II", @"III", @"IV", nil]; for (int i=0; i<[myArray count]; i++) { float ycord = i*40.0; CGRect lblframe = CGRectMake(10.0, ycord, 200.0, 20.0); UILabel *label = [[UILabel alloc] initWithFrame:lblframe]; label.backgroundColor = [UIColor blackColor]; label.font = [UIFont systemFontOfSize:17]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor colorWithRed:(188/255.f) green:(149/255.f) blue:(88/255.f) alpha:1.0];; label.text = [myArray objectAtIndex:i]; [self.view addSubview:label]; [label release]; } 

I hope this helps, but providing more information about what you are trying to do may help you.

0
source

All Articles