【问题标题】:Check if an UIImageView is on subview检查 UIImageView 是否在子视图上
【发布时间】:2013-06-14 10:44:50
【问题描述】:

在我的应用程序中,我需要多次调用UIViews。但在我的一种方法中,我有一个类似的代码:

[self addSubview:UIImageView];

但我读过addsubview 方法必须调用一次。那么,要让代码怎么样,我如何检查它是否已经在 subview 上?喜欢:

if ([UIImageView isOnSubview] == NO)
{
    [self addSubview:UIImageView];
}

因为我没有找到任何方法来检查这个:/

谢谢!

【问题讨论】:

  • 为什么addSubview 应该只被调用一次?如果是你说的……
  • if ([UIImageView superView]){ //给你 }
  • UIImageViewUIImageView 对象的实例还是 UIImageView 表示 UIImageView 类?您对对象的命名非常糟糕。你应该命名一些已经是类名的东西。
  • @Lucien : addSubview 方法应该只调用一次,我已经在其他主题上看到过很多次了。
  • @meccan :不,因为您需要添加一次图像视图。

标签: ios objective-c uiimageview subview


【解决方案1】:

您可能正在寻找在UIView class reference 中拍摄的 UIView 的-(BOOL)isDescendantOfView:(UIView *)view;

【讨论】:

  • 但是在这种情况下我该如何使用它呢?
  • [imageView isDescendantOfView:self.view]
  • @Adithya 我无法在我的解决方案中应用您的过滤(答案)。如果您不介意可以用一个示例代码详细说明答案 - 提前致谢
【解决方案2】:

用这个

 for (UIView *subview in [self subviews])
 {
     NSLog(@"%@", subview);
     // ---------- remember one thing there should be one imageview ------
     if(![subview isKindOfClass:[UIImageView class]])
     {
           [self addSubview:UIImageView];
     }
 }

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多