Checking if UIView has been removed?

Discussion in 'iOS Programming' started by Danneman101, Aug 9, 2010.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    I'm wondering if there is a method to check if a UIView has been removed from its superview or if it's still there. I'm using "removeFromSuperview" to remove the UIView, and even release it from memory.

    Code:
    [myView removeFromSuperview];
    [myView release];
    
    I know I can simply create a boolean that keeps track of it by setting it at the same time the view loads/unloads, but just thought this would be neater.

    What I've tried so far is to check it's existence using this code:

    Code:
    if (myView) {}
    
    But it always returns true even though myView has been removed.

    If it's any help, the view has been created entirely by code and has no nib-file.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    After you release it you could set the variable to nil. You can then easily check if myView == nil. Note that if myView is a property then care is required. This would result in an over-release (so don't do this).

    Code:
    @property (retain) NSView *myView;
    
    ...
    
    @synthesize myView
    
    ...
    
    [myView removeFromSuperview];
    [myView release];
    self.myView = nil;
    
    In this case the release is unnecessary: the property setter will release for you.
     
  3. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    Ah, I thought that release would do it, but setting it to nil did the trick. Thanks a lot :)
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    release simply decrements the retain count by 1. As you do not pass the address of the pointer and do not assign anything to the pointer there is no way that the method call could update the address pointed to.
     
  5. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #5
    If you subclass UiView you can implement the didMoveToWindow method, or if it has a view controller there is the viewDidDisappear method (though this might just get called when the view leaves the screen, not when it is removed from its superview, you'd have to check)
     
  6. macrumors newbie

    Joined:
    Jan 11, 2012
    #6
    check superview nil

    You can check
    if ([<yourUIView> superview] == nil)

    If it is nil, that means the UIView is not in view, in case u are using addSubView to show the view.
     

Share This Page