嗨,在我的应用程序中,我有一个数组。因为我有很多UIView和UIImageView组件,我想从该数组中获取每个组件,并且必须知道是图像视图还是视图,如果是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"];
}
}
答案 0 :(得分:7)
使用isKindOfClass
示例:
if ( [originalValue isKindOfClass:[UIImageView class]] ){
UIImageView *myImageView = (UIImageView *)originalValue;
}
答案 1 :(得分:-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]