简体   繁体   English

Objective-C:我认为UILabel被“卡住”了

[英]Objective-C: UILabel “stuck” in my view

I'm working on a tabbed app that (among other things) creates a simple Facebook feed. 我正在开发带标签的应用程序(除其他功能外)可创建一个简单的Facebook feed。 The Facebook section is a UINavigationController holding a Master Table View and a Detail View. Facebook部分是一个UINavigationController包含一个主表视图和一个详细视图。 When I click one of the rows in the Master Table View, it brings up the detail view (and the appropriately filled labels) as intended. 当我单击“主表视图”中的某一行时,它将按预期显示详细视图(以及适当填充的标签)。 However, if I go back to the Master View and select another row, something strange happens. 但是,如果我回到主视图并选择另一行,则会发生一些奇怪的情况。 If the "detailLab" label (which is sizeToFit 'd) was, say, four lines long on the first occurrence of Detail View, and the "detailLab" label was 2 lines long on the second occurrence of Detail View, then on the second occurrence of Detail View, "detailLab" will display the first two lines properly, but underneath them will be the last two lines from the first occurrence of Detail View. 如果“ detailLab”标签(为sizeToFit )在第一次出现的“ Detail View”上长四行,而“ detailLab”标签在第二次出现的“ Detail View”上长2行,则在第二次出现出现细节视图时,“ detailLab”将正确显示前两行,但在它们下面将是第一次出现细节视图的后两行。

A picture of this happening . 发生这种情况的画面

And some (hopefully) relevant code: 和一些(希望)相关的代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGRect labelFrame = CGRectMake(100, 40, 220, 150);
    detailLab = [[UILabel alloc] initWithFrame:labelFrame];
    [detailLab setTextColor:[UIColor darkGrayColor]];
    detailLab.font = [UIFont systemFontOfSize:14];

    [detailLab setText:detail];
    [detailLab setNumberOfLines:0];
    [detailLab sizeToFit];

    [self.view addSubview:detailLab];

    NSURL *url = [NSURL URLWithString:pictureString];
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
    picture = [UIImage imageWithData:imageData];
    bigImage.image = picture;
    NSLog(@"dateString: %@", dateString);
    dateLabel.text = dateString;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    bigImage = nil;
    detailLab = nil;
    detailLab.hidden = YES;
    // Release any retained subviews of the main view.
    self.detailDescriptionLabel = nil;
}

If you need any other code or details, just ask; 如果您需要任何其他代码或详细信息,请询问; I'd be happy to provide it. 我很乐意提供。

The problem is that each time viewWillAppear is called you are creating a new label without, I assume, removing it in viewWillDisappear . 问题在于,每次调用viewWillAppear ,您都在创建一个新标签,而我认为没有在viewWillDisappear中将其删除。 So the labels are stacking up, one on top of the other. 因此,标签堆叠在一起,一个堆叠在另一个之上。

Remember you may get called like this (ignoring irrelevant calls): 请记住,您可能会这样被打(忽略无关的呼叫):

viewDidLoad

viewWillAppear
viewWillDisappear

viewWillAppear        <- Whoops! Two labels
viweWillDisappear

viewDidUnload

A neater solution would be to add your label view in viewDidLoad and only alter the position of that already existing view in viewWillAppear . 更为巧妙的解决方案是在viewDidLoad添加标签视图,并仅更改viewWillAppear中已经存在的视图的位置。 You still need to remember to dispose of the view in viewDidUnLoad . 您仍然需要记住在viewDidUnLoad处置视图。

Here's what I mean (note this was only eyeball compiled so you may need to correct some typos): 这就是我的意思(请注意,这只是眼球而已,因此您可能需要纠正一些错别字):

- (void)viewDidLoad
{
    [super viewDidLoad];
    detailLab = [[UILabel alloc] initWithFrame:CGRectZero];
    [detailLab setTextColor:[UIColor darkGrayColor]];
    detailLab.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:detailLab];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGRect labelFrame = CGRectMake(100, 40, 220, 150);
    [detailLab setFrame:labelFrame];
    [detailLab setText:detail];
    [detailLab setNumberOfLines:0];
    [detailLab sizeToFit];

    NSURL *url = [NSURL URLWithString:pictureString];
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
    picture = [UIImage imageWithData:imageData];
    bigImage.image = picture;
    NSLog(@"dateString: %@", dateString);
    dateLabel.text = dateString;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    bigImage = nil;
    detailLab = nil;
    detailLab.hidden = YES;
    // Release any retained subviews of the main view.
    self.detailDescriptionLabel = nil;
}

That is normal with Master View to Detail Views - Frustrating, but Normal. 从主视图到详细视图,这很正常-令人沮丧,但正常。 The solution I found when I had the problem was to reset the label at the top of each -ViewDidLoad. 我遇到问题时找到的解决方案是重置每个-ViewDidLoad顶部的标签。

One option would be to remove it in viewDidUnload. 一种选择是在viewDidUnload中将其删除。 Another would be to reset the label each time, using a segment of code like the one below, positioned above the section of your code where it actually loads in the content 另一个方法是每次使用以下代码段(位于代码部分实际位于内容中的上方)重置标签,

detailLab = [NSString stringWithFormat:@""]; detailLab = [NSString stringWithFormat:@“”];

The issue is that with the setup of most Master/Detail views, it's not actually creating new detail views each time you add a view, it's just creating another link to the same view in the master view section. 问题在于,随着大多数主视图/明细视图的设置,实际上并不是每次添加视图时都会创建新的明细视图,而只是在主视图部分中创建了指向同一视图的另一个链接。 This is fine, but you just need to remember to use viewDidUnload lots 很好,但是您只需要记住使用viewDidUnload很多

Hope it Helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM