views:

47

answers:

2

Basically I want to remove all objects from a UIScrollView and I haven't yet found a solution to it because a simple "removeAllObjects" command doesn't work. Does anyone have an idea how to do it?

+3  A: 

The easiest way:

for(UIView *subview in [scrollView subviews]) {

    [subview removeFromSuperview];

}
iWasRobbed
Is it safe to modify the array contents while looping thru the elements? I'm just wondering...
progrmr
In general it is probably not the best practice, so you may want to set up a temporary array marked for removal. :P
iWasRobbed
It's definitely safe to remove subviews while enumerating parent's subviews collection
ohhorob
+2  A: 

Even easier:

[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
TomH
i'm guessing you have the same issue with `makeObjectsPerformSelector` in regards to it looping through the subviews while modifying the array concurrently. this way is elegant and used by many, but how do you address what progrmr brought up above?
iWasRobbed
I'm fairly sure makeObjectsPerformSelector: generates a temporary array before calling the selector on each object. If it didn't, as you say, it'd cause issues.
Kalle