开发者

View is not gone, sometimes

There is a very hard issue I'm trying to pursue. In the viewDidAppear: I have the following code:

if(dataSourceCount > 0)
{
    [scrollView setHidden:NO];
    UIView *ndView = [self.view viewWithTag:204];
    [ndView removeFromSuperview];
    self.noDataV开发者_JAVA技巧iew = nil;
    infoBtn.hidden = NO;
}
else
{
    [scrollView setHidden:YES];
    [[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil];
    self.noDataView.tag = 204;
    [self.view addSubview:self.noDataView];
    infoBtn.hidden = YES;
}
[super viewDidAppear:animated];

The problem occurs on if true case, very rarely and as a result on the device I can see the view that has been removed from superview - ndView.

I was thinking that viewWithTag may return nil sometimes, but this is not the case as I found out from debug. Also tried to move self.noDataView = nil to else and found the issue again.

Is there any obvious or non-obvious mistake that I'm doing here, which I'm not supposed? The idea of this code snippet is to temporarily show some other view, while data is not available.


I don't know if that solves your problem but:

  1. [super viewDidApper:animated] should be on the firstline within your viewDidAppear Method
  2. I'd move your code to viewWillAppear

Maybe this helps a bit.


Submitting this as an answer rather than a comment

'What happens if the false case happens twice?, when you leave the view, does it remove the view from its superview?, if your navigation controller doesnt deallocate your viewController, i think it may preserve subviews that were added? First thought thats popped into my head'

The problem is that your subview was being added multiple times, and removed once. Thus when you expected it to be removed, it was only removing the top one.

I'd recommend having the view as an instance variable, so that every time you add and remove the subview, it is pointing to the same one.


if (!self.noDataView)
{
  [[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜