Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Ah, I thought that release would do it, but setting it to nil did the trick. Thanks a lot :)
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors 6502
Dec 22, 2009
471
1
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

macrumors newbie
Jan 11, 2012
1
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.