0

嗨,在我的应用程序中,我有一个数组。因为我有许多 UIView 和 UIImageView 组件,我想从该数组中获取每个组件,并且必须知道是 imageview 还是 view,如果那是 imageview,我必须更改该 imageview 的图像。但我没有知道如何知道组件是图像视图还是视图。如果有人知道,请告诉我识别组件类型的方法。请在这个问题上帮助我。

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"];
    }
}
4

2 回答 2

7

利用isKindOfClass

例子:

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

   UIImageView *myImageView = (UIImageView *)originalValue;

}
于 2013-03-16T17:59:20.560 回答
-1

您可以通过类内省检查

尝试这个

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

   //same class
}

一些你可能觉得方便的额外东西

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]
于 2013-03-16T17:59:14.630 回答