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