tags:

views:

47

answers:

4

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.noDataView = 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.

+1  A: 

I can't see anything wrong with that code but I do have another question :

If you're storing your no data view as self.noDataView why are you bothering to set it's tag? Why isn't your code this :

if(dataSourceCount > 0)
{
    [scrollView setHidden:NO];
    [self.noDataView removeFromSuperview];
    self.noDataView = nil;
    infoBtn.hidden = NO;
}
else
{
    [scrollView setHidden:YES];
    [self.view addSubview:self.noDataView];
    infoBtn.hidden = YES;
}
[super viewDidAppear:animated];

and what's with the line

[[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil];

you seem to be just loading a nib and then ignoring the result or are there some side effects of this that you're using?

deanWombourne
It's an outlet, owner is self so it's connected to `noDataView`. http://developer.apple.com/mac/library/documentation/cocoa/reference/ApplicationKit/Classes/NSBundle_AppKitAdditions/Reference/Reference.html#//apple_ref/occ/clm/NSBundle/loadNibNamed:owner:
Michael
Ah yea, I'd not noticed the owner parameter. Sorry!
deanWombourne
+1  A: 

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

cheers

sam

samsam
+1  A: 

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.

Bongeh
+1  A: 
if (!self.noDataView)
{
  [[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil];
}
tc.