ios – 使用isKindOfClass:我不明白为什么这段代码行为这样

-(void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(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");
      }
   }
}

我预计输出是“元素是一个UIImageView,但它实际上是元素不是一个UIImageView,为什么不是还有其他的子视图,只有一个,而且运行时,图像显示为100,而不是500,500如预期.

解决方法

你的支票是错误的您应该调用isKindOfClass:on对象而不是对象类.
[element isKindOfClass:[UIImageView class]]

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...