25

I was wondering how to find out if a subview (in my case pageShadowView) has already been added to my view.

I've come up with this, but it doesn't really work:

if ([pageShadowView isKindOfClass:[self.view class]]) {
        [self.view addSubview:pageShadowView];
    }

Also, I'm still confused about the self.-thing. I know that this has to do with making clear that we are talking about the view of the current ViewController ... but do I really need it if there (1) are no other ViewControllers or (2) if it doesn't really matter because if I ever wanted to refer to another viewController, I'd make sure to call it?

I'm sorry if this is all very basic, but I'd be very grateful for your comments.

n.evermind
  • 11,944
  • 19
  • 78
  • 122

8 Answers8

84

Here:

BOOL doesContain = [self.view.subviews containsObject:pageShadowView];

And yes, you need this self. There is no explicit ivar "view" on UIViewController. The self.view statement is actually a call on method [self view] which is a getter for UIViewController's view.

Bartosz Ciechanowski
  • 10,293
  • 5
  • 45
  • 60
  • Thanks a lot. I tried to abbreviate this a bit, but don't get it to work. I tried `if ([self.view.subviews containsObject:pageShadowView]=! TRUE) {[self.view addSubview:pageShadowView];}` but I only get the error "Lvalue required as left operand of assignment". Sorry, I'm still a beginner and guess that I got something very basic simply wrong. – n.evermind Mar 31 '11 at 16:49
  • 1
    In Objective-C, there is YES and NO. You can skip 'true' test altogether: if ( ! [self.view.subviews containsObject:pageShadowView]) {[self.view addSubview:pageShadowView];} – Bartosz Ciechanowski Mar 31 '11 at 16:51
  • "there is no explicit ivar "view" on UIViewController" > That is not totally correct. There is an ivar `UIView *_view`, but it is not accessible to us (third party devs) since it in the @package section. – nacho4d Jul 23 '13 at 14:17
  • thanks @BartoszCiechanowski, In case of window, I think it is fail, any other option to handle the same? – Gopal Devra Nov 27 '17 at 07:25
25

Give it a unique tag: view.tag = UNIQUE_TAG, then check the container view for existence:

BOOL alreadyAdded = [containerView viewWithTag:UNIQUE_TAG] != nil;
coneybeare
  • 33,113
  • 21
  • 131
  • 183
  • Thanks. I'm checking if an Async Image View had been added to a custom Collection View cell, and this check works perfectly! – David Douglas Jun 04 '13 at 09:24
8

you can find a sub view like this

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIView class]])
    {
        //here do your work
    }
}
Vivek Sehrawat
  • 6,560
  • 2
  • 26
  • 39
saadnib
  • 11,145
  • 2
  • 33
  • 54
7

There's one more way to find, in Swift: isDescendant(of view: UIView) -> Bool or in Obj-C: - (BOOL)isDescendantOfView:(UIView *)view

Swift:

    if myView.isDescendant(of: self.view) {
        //myView is subview of self.view, remove it.
        myView.removeFromSuperview()
    } else {
        //myView is not subview of self.view, add it.
        self.view.addSubview(myView)
    }

Obj-C:

if([myView isDescendantOfView:self.view]) {   
    //myView is subview of self.view, remove it.
    [myView removeFromSuperView];
} else {
    //myView is not subview of self.view, add it.
    [self.view addSubView:myView];
}
Hemang
  • 26,840
  • 19
  • 119
  • 186
2

SWIFT VERSION:

let doesContain = self.view?.subviews.contains(pageShadowView)
Dave Levy
  • 1,036
  • 13
  • 20
2

best and easy way to find that your view is exist or not , there are many way like you check a view is contain or not in super view or just view some time this method is failed because if the Uiview is already remove then error occur, so code is here : here errorView is my UiView

 errorView.tag = 333

 if ( self.view?.viewWithTag(333) != nil ){
    print("contain")
 }

 else {
    print("not contain")
 }
Muhammad Ahmad
  • 388
  • 4
  • 9
1

To add to what coneybeare said, you could do the following. If you set your object.tag=100;

           if ([self.view.superview viewWithTag:100] == nil){ //if statement executes if the object with tag 100 in view.superview is absent (nil)

           if ([self.view viewWithTag:100] == nil){ //if statement executes if the object with tag 100 in view (not superview) is absent (nil)
user3344717
  • 161
  • 3
  • 5
0

add a retain value of the view

then check the retain value

if > 1 , then exist , if perfect should be 2

then release it once

chings228
  • 1,859
  • 24
  • 24