我在循环中创建多个标签,并且我对内存管理感到困惑,因为标签的每个实例都具有相同的名称,因为它们都是在循环内创建的,这意味着每次在标签内创建新标签循环,分配的最后一个标签将被覆盖,不再占用内存或者它仍然存储在内存中。相当困惑,请提前帮助,谢谢。
- (void)fetchEntrys
{
JournalrAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Entrys"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
if ([objects count] == 0) {
NSLog(@"No matches");
} else {
NSLog(@"Matches found");
matches = objects[1];
NSString *firstEntry = [matches valueForKey:@"entry"];
NSLog(@"First Entry: %@", firstEntry);
self.totlal = [objects count];
int i = [objects count] - 1;
NSLog(@"%i", i);
while ( i > -1) {
matches = objects[i];
NSString *entry = [matches valueForKey:@"entry"];
NSDate *date = [matches valueForKey:@"date"];
NSDateFormatter *formatDate = [[NSDateFormatter alloc]init];
[formatDate setDateFormat:@"MM/dd/YY"];
NSString *dateString = [formatDate stringFromDate:date];
NSLog(@"Date: %@", dateString);
NSLog(@"Entry: %@", entry);
UILabel *dateLabel = [[UILabel alloc] init];
dateLabel.text = dateString;
dateLabel.frame = CGRectMake(20, cY, 100, 30);
dateLabel.numberOfLines = 3;
dateLabel.font = [UIFont boldSystemFontOfSize:24.0];
[dateLabel setTag:i];
UIButton *deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(230, dateLabel.frame.origin.y, 70, 27)];
[deleteButton addTarget:self action:@selector(deleteButtonPressed) forControlEvents:UIControlEventTouchDown];
[deleteButton setBackgroundImage:[UIImage imageNamed:@"delete.jpg"] forState:UIControlStateNormal];
[deleteButton setTag:i];
cY += 35;
UILabel *labelEntry = [[UILabel alloc]init];
labelEntry.numberOfLines = 0;
labelEntry.text = entry;
CGRect lblFrame = CGRectMake(20, cY, 280, 1000);
labelEntry.frame = lblFrame;
labelEntry.textAlignment = NSTextAlignmentLeft;
labelEntry.backgroundColor = [UIColor clearColor];
[labelEntry sizeToFit];
[labelEntry setTag:i];
UILabel *outerLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, cY - 50, 300, labelEntry.frame.size.height + 80)];
[scrollView addSubview:outerLabel];
outerLabel.layer.borderColor = [UIColor grayColor].CGColor;
outerLabel.layer.borderWidth = 1.0;
outerLabel.tag = i;
if(i ==[objects count] - 1){
self.firstY = outerLabel.frame.origin.y;
}
//UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, cY, 312, labelEntry.frame.size.height)];
//backgroundImage.contentMode = UIViewContentModeScaleToFill;
//backgroundImage.image = [UIImage imageNamed:@"post.jpg"];
//NSLog(@"Image Height :%f", backgroundImage.frame.size.height);
//[scrollView addSubview:backgroundImage];
cY += labelEntry.frame.size.height;
NSLog(@"Label Height: %f", labelEntry.frame.size.height);
NSLog(@"currenty: %f", cY);
cY += 100.f;
scrollView.scrollEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
[scrollView addSubview: labelEntry];
[scrollView addSubview:dateLabel];
[scrollView addSubview:deleteButton];
NSLog(@"%i", i);
i--;
}
scrollView.contentSize = CGSizeMake(320, cY);
NSLog(@"cY: %f", cY);
self.currentI = i;
答案 0 :(得分:0)
只要仍然有一些标签的引用,它的内存将被保留。例如,如果您使用[self.view addSubview:label]
,那么只要self.view
,标签就会保留在内存中。
同样,如果将标签添加到数组中,标签的内存将被保留,直到释放te数组。
创建具有相同实例名称的新标签后,您将无法再通过它访问旧标签。如果您的标签是视图的子视图,则可以使用[view subviews]
返回子视图数组并使用该数组查找标签。