Question

I have one scrollView named svCreateTask. In this scrollView I put UITextField, UITextView, UIButtons statically and UIView programmatically on button press. Now I want to remove Selected UIView on button press. and The view remove from scrollView whose tag is the same as Button tag.

And the if statement check every Time TRUE so other subview whose tag is same as my button tag also remove from scrollView. I want to remove only UIView.

Here I post My Code:

 NSLog(@"Enter in removeNotification method.");

    UIButton *btn = (UIButton *)sender;
    NSLog(@"btn Tag = %d",[btn tag]);

    NSArray *viewsToRemove = [svCreateTask subviews];

    for (int i=0; i<viewsToRemove.count; i++)
    {
        NSLog(@"Class == %@",[[viewsToRemove objectAtIndex:i] class]);
        if ([[viewsToRemove objectAtIndex:i]isKindOfClass:[UIView class]])
        {
            NSLog(@"In Class Check...");
            UIView *v =[viewsToRemove objectAtIndex:i];
            if (v.tag == btn.tag)
            {
                [v removeFromSuperview];
            }
        }

    }

and my NSLog is.

2014-05-13 14:49:42.769 TOPDesign[379:11303] Enter in removeNotification method.
2014-05-13 14:49:42.770 TOPDesign[379:11303] btn Tag = 1
2014-05-13 14:49:42.771 TOPDesign[379:11303] Class == UILabel
2014-05-13 14:49:42.772 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.773 TOPDesign[379:11303] Class == UITextView
2014-05-13 14:49:42.773 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.774 TOPDesign[379:11303] Class == UITextField
2014-05-13 14:49:42.775 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.775 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.776 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.777 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.777 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.778 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.779 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.780 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.780 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.781 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.782 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.782 TOPDesign[379:11303] Class == UIButton
2014-05-13 14:49:42.783 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.783 TOPDesign[379:11303] Class == UILabel
2014-05-13 14:49:42.784 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.784 TOPDesign[379:11303] Class == UILabel
2014-05-13 14:49:42.785 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.786 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.786 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.787 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.787 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.788 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.788 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.789 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.789 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.790 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.799 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.801 TOPDesign[379:11303] Class == UIView
2014-05-13 14:49:42.801 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.802 TOPDesign[379:11303] Class == UIView
2014-05-13 14:49:42.802 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.803 TOPDesign[379:11303] Class == UIImageView
2014-05-13 14:49:42.803 TOPDesign[379:11303] In Class Check...
2014-05-13 14:49:42.804 TOPDesign[379:11303] Class == UIView
2014-05-13 14:49:42.805 TOPDesign[379:11303] In Class Check...

My screen structure is.

UIView(main view) > ScrollView > TextField,TextView,Label, Buttons, UIView Etc.

I want to get UIView Which is a subview of my scrollView.

Was it helpful?

Solution 2

What you want is to use the method isMemberOfClass: instead. (Check out Apple Documentation for isMemberOfClass method)

isMemberOfClass:- Returns a Boolean value that indicates whether the receiver is an instance of a given class. (required)

Whereas (Check out Apple Documentation for isKindOfClass: method)

isKindOfClass:- Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)

So basically when you do your isKindOfClass: check it is returning TRUE for any subclass of UIView so this includes UIButton, UILabel, etc

So change if ([[viewsToRemove objectAtIndex:i]isKindOfClass:[UIView class]]) to if ([[viewsToRemove objectAtIndex:i] isMemberOfClass:[UIView class]])

An example of the inheritance for UILabel is like UILabel>> UIView >> UIResponder >> NSObject. So when you do isKindOfClass:[UIView class] this will return TRUE for UILabel because UILabel as you can see is a subclass of UIView but if we did isMemberOfClass:[UIView class] we ignore any subclassed objects like UILabel, so this will return FALSE for UILabel but still return TRUE for UIView.

Another solution would be when creating your UIVeiws give them a tag so when it comes to removing them you can just get the specific view you want to remove using viewWithTag:.

OTHER TIPS

isKindOfClass returns true for any derived classes as well. Because all user interface related classes are derived from UIView, this method returns true for all objects in your case.

Use code like this, if you just want to test for a UIView class:

if ([obj class] == [UIView class]) {
}

This code just checks for objects of the type UIView.

The problem with you is that every thing in your view hierarchy is having UIView as its base class, so you are getting True every time. One possible solution to deal with this is as follows:

Give some what special tag to your UIView as if normally you are giving tag as 3, then you can just multiply its tag with 1000, now on getting True, you can calculate that if a UIView is containing three 0's, then remove it else keep it :)

Here is another way.

UIView *v;
for (int i=0; i<viewsToRemove.count; i++)
{
    v = [viewsToRemove objectAtIndex:i]
    if ([NSStringFromClass([v class]) isEqualToString:@"UIView"])
    {
        //Remove view
    }
}

Among the other answer's, I think using the isMemberOfClass mentioned by Popeye is the right way to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top