简体   繁体   中英

IPHONE: Test to see if a view is on the superview

I am about to do a

[vistaX removeFromSuperview];

How do I test to see if vistaX is present on the super view before removing it? Removing a view that isn't there would lead to a crash on the application...

thanks for any help.

You can guard it with:

if(vistaX.superview)
     [vistaX removeFromSuperview];

Although, I wasn't aware that removeFromSuperview would fail if there wasn't a superview. Are you sure that this is the issue and it isn't maybe related to the fact that removeFromSuperview releases the view?

EDIT : Based on your comment below, it sounds like vistaX's retain count is going to 0 the first time around and it's being freed. If you don't want this to happen, add a property to your class that retains vistaX (ie "@property (retain)") so you can be sure that it'll always be around.

EDIT EDIT : Do you have a handle on the superView or one of its ancestors? If so, I would recommend setting a unique tag on the vistaX view. This can be done programmatically or through IB. Then, use the viewWithTag selector on one of the ancestors to search for the vistaX view by its unique tag.

You Can Check it By applying one condition like

if(vistaX.superview!=nil)
   [vistaX removeFromSuperview];

then if superview exists, it will remove from super view, otherwise not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM