How to check wether the component is UIView or UII

2019-08-28 11:51发布

问题:

Hi in my application i have an array. In that i have number of UIView and UIImageView Components, and i want to fetch each component from that array and have to know wether that is imageview or view, if that is imageview i have to change the image for that imageview.But i dont have any idea how to know wether the component is imageview or view. If any one know please let me know the way to recognize the component type. Please help me in this issue.

for (int i=0; i<[array count]; i++){
   // here i have to know wether the component is imageview or view and based on that i have to do below operations 
   UIView *view1=[array objectAtIndex:i];
   NSLog(@"%@",[array objectAtIndex:i]);
   if (130==view1.tag){
       view1.backgroundColor=[UIColor redColor];
    }
    UIImageView *image1=[array objectAtIndex:i];
    if (132==image1.tag){
       image1.image=[UIImage imageNamed:@"Approve2.png"];
    }
}

回答1:

Use isKindOfClass

Example:

if ( [originalValue isKindOfClass:[UIImageView class]] ){

   UIImageView *myImageView = (UIImageView *)originalValue;

}


回答2:

You can check by class introspection

Try this

if( [obj1 class] == [obj2 class] ){

   //same class
}

Few extra things that you may feel handy

Class Introspection
·  Determine whether an objective-C object is an instance of a class
        [obj isMemberOfClass:someClass];
·  Determine whether an objective-C object is an instance of a class or its descendants
        [obj isKindOfClass:someClass];
·  The version of a class
        [MyString version]
·  Find the class of an Objective-C object
        Class c = [obj1 class]; 
        Class c = [NSString class];
·  Verify 2 Objective-C objects are of the same class
        [obj1 class] == [obj2 class]


标签: iphone uiview