Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using isKindOfClass: I don't understand why this code is behaving this way

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

like image 455
Victor Engel Avatar asked Nov 29 '12 05:11

Victor Engel


1 Answers

Your check is wrong. You should call isKindOfClass: on object and not on class of object.

[element isKindOfClass:[UIImageView class]]
like image 193
Rahul Wakade Avatar answered Nov 04 '22 18:11

Rahul Wakade