2

I check if view is already added/exist, then I need to remove it

if([self.upgradeView superview])
        [self.upgradeView removeFromSuperview];

self.upgradeView is added on self.view. but condition returns false and self.upgradeView is never removed.

Is there anything that is missed?

=========================UPDATE====================================

I have added this code in viewwillappear just below the code used to add self.upgradeView on self.view like this

if ([isLoggedInPremiumUser isEqualToString:@"0"])
{
    [self createUpgradeFooterView];
}
else
{
    if([self.upgradeView superview])
        [self.upgradeView removeFromSuperview];
}
Praful Kadam
  • 372
  • 6
  • 22

3 Answers3

0

When are you doing this if statement? It could be that when this is called the view has not yet been added.

Please provide more of your code including where you add the view and where this if statement is called.

chrissukhram
  • 2,957
  • 1
  • 13
  • 13
0

You may want to use UIView's -(BOOL)isDescendantOfView:(UIView *)view; to check if view is added or not.

if(![[self upgradeView] isDescendantOfView:[self view]]) { 
    [self.view addSubview:[self upgradeView]];
} else {
    [[self upgradeView] removeFromSuperview];
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

I had the same problem and was trying to search any answer online but found none. Then I found this but no solution neither. I tried some debugs to print superview at different location in the code. I found out that at first this value is nil but then it's not.

So I think this problem is due to the UIView is not completed loaded, then I searched several topics on how to check if a UIView is loaded. Finally I think I get the solution. If I check the superview property in layoutSubviews function, and set all other properties there. Everything works great!

I know this is an old post, just in case someone is having the same headache.

Valar Morghulis
  • 647
  • 5
  • 16