简体   繁体   中英

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. So I loop through the array, adding each UIImageView to the scene as follows:

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

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:

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

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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