-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.image = [UIImage imageNamed:@"Sample.png"];
[self.view addSubview:imageView];
NSArray *subviews = [self.view subviews];
for(id element in subviews) {
if ([[element class] isKindOfClass:[UIImageView class]]) //check if the object is a UIImageView
{
NSLog(@"element is a UIImageView\n");
[element setCenter:CGPointMake(500., 500.)];
} else {
NSLog(@"element is NOT a UIImageView\n");
}
}
}
I expected the output to be "element is a UIImageView, but it's actually element is NOT a UIImageView. Why? It's not that there are other subviews. There is only one. Furthermore, when run, the image is displayed at 100,100, not 500,500 as expected.
Your check is wrong. You should call isKindOfClass:
on object and not on class of object.
[element isKindOfClass:[UIImageView class]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With