PDA

View Full Version : Checking if UIView has been removed?




Danneman101
Aug 9, 2010, 06:36 AM
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.


[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:


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.



robbieduncan
Aug 9, 2010, 06:52 AM
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).


@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.

Danneman101
Aug 9, 2010, 07:23 AM
Ah, I thought that release would do it, but setting it to nil did the trick. Thanks a lot :)

robbieduncan
Aug 9, 2010, 07:26 AM
Ah, I thought that release would do it, but setting it to nil did the trick. Thanks a lot :)

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.

seepel
Aug 9, 2010, 08:00 AM
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)

RSharma5
Jan 11, 2012, 02:22 AM
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.