7
7
7j72014-10-25 16:52:03
Objective-C
7j7, 2014-10-25 16:52:03

How to loop through child values?

Good time of the day, you need to sort through and delete all child elements, now I have such a horror:

for view1 in self._searchBackground.subviews {
    for view2 in view1.subviews {
        for view3 in view2.subviews {
            for view4 in view3.subviews {
                for view5 in view4.subviews {
                    for view6 in view5.subviews {
                        view6.removeFromSuperview()
                    }
                    view5.removeFromSuperview()
                }
                view4.removeFromSuperview()
            }
            view3.removeFromSuperview()
        }
        view2.removeFromSuperview()
    }
    view1.removeFromSuperview()
}
self._searchBackground.removeFromSuperview()

how to implement it in a function is written in the swift language, you can have examples in any other, or maybe there are already some solutions. It's just too expensive to add a branch each time.
P.S. Thank you in advance!!!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SlivTime, 2014-10-25
@SlivTime

Use recursion. I am not familiar with swift, but in Python I would do something like this:

def remove_children_subviews(view):
    view.remove_from_superview()
    children = getattr(view, 'subviews', [])
    map(remove_children_subviews, children)

A
An, 2014-10-25
@Flanker_4

I don't see the point in deleting all subviews for child elements, IMHO only the top one is enough, i.e. something like this
[view.subviews makeObjectsPerformSelector: @selector (removeFromSuperview)];
But if you really need everything, then something like that.

-(void) removeSubviews:(UIView*) view{
     for (UIView *child in view.subview){
           [self removeSubviews:child];
     }
     [view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

A
AxisPod, 2014-10-27
@AxisPod

- Dad, dad, do destructors exist?
- No son, it's fantastic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question