简体   繁体   English

将UIView添加到超级视图/作为子视图后访问UIView

[英]Access UIView after adding it to superview/as a subview

I have a variable sized array of UIImageViews that I'd like to add to my main View. 我有一个可变大小的UIImageViews数组,我想添加到我的主视图中。 So I loop through the array, adding each UIImageView to the scene as follows: 因此,我遍历数组,将每个UIImageView添加到场景中,如下所示:

for (UIImageView *nextImageView in myImageArray]
    [self.view addSubview:nextImageView];

(I also configure their frames individually but have left that out here) (我也分别配置了它们的框架,但这里省略了)

However, I later want to remove these UIImageViews. 但是,我以后想删除这些UIImageViews。 But how can I access them? 但是我该如何访问它们? I'm aware of the tag property, and that I could tag each one as I add it, and then remove views of the same tag, however I'm also adding and removing other elements to the view which messes up the index, and it doesn't seem like a very foolproof solution. 我知道tag属性,可以在添加每个属性时对其进行标记,然后删除同一标签的视图,但是我还要在视图中添加和删除其他元素,从而弄乱了索引,并且这似乎不是一个非常简单的解决方案。

I thought of having a single UIView property in my class, and adding the array of UIImageViews as subviews of this main view, like so: 我想到了在类中具有一个UIView属性,并将UIImageViews数组添加为该主视图的子视图,如下所示:

for (UIImageView *nextImageView in myImageArray]
    [holderView addSubview:nextImageView];

Then I can remove all the images by calling: 然后,我可以通过调用以下命令删除所有图像:

[holderView removeFromSuperview];

However, I'm not sure how to arrange the UIImageViews within the UIView. 但是,我不确定如何在UIView中安排UIImageViews。 If I try to configure their frames individually, they don't appear. 如果我尝试分别配置它们的框架,它们不会出现。 So at the minute they just draw over the top of each other. 因此,此时此刻,它们只是彼此重叠。 Ideally I'd like them to appear in a stack. 理想情况下,我希望它们出现在堆栈中。 Any ideas how I might do that? 有什么想法我该怎么做吗?

Thanks :) 谢谢 :)

I believe you could iterate the array of UIImageViews and call removeFromSuperview 我相信您可以迭代UIImageViews的数组并调用removeFromSuperview

for (UIImageView *nextImageView in myImageArray]
    [nextImageView removeFromSuperview];

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM