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
2010-07-12 19:22:11
Is it safe to modify the array contents while looping thru the elements? I'm just wondering...
progrmr
2010-07-12 21:40:15
In general it is probably not the best practice, so you may want to set up a temporary array marked for removal. :P
iWasRobbed
2010-07-12 21:54:08
It's definitely safe to remove subviews while enumerating parent's subviews collection
ohhorob
2010-07-22 04:44:03
+2
A:
Even easier:
[[scrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
TomH
2010-07-13 02:34:05
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
2010-07-13 02:41:35
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
2010-07-16 07:53:58