Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
for (UIView *view in self.subviews) { [view removeFromSuperview]; }
The above gave me the Warning message of "UIView may not respond to -countByEnumerationWithState:objects:count: And my code crashed when I tried to run it.
I used this and it worked for me:
// Assuming *view is already set up as a UIView...
for (int i = 0; i < [[self.view subviews] count]; i++ ) { [[[self.view subviews] objectAtIndex:i] removeFromSuperview]; }
The first one failed because subviews is not a property of self, which is presumably your view controller. If this was a custom UIView subclass, it would probably work.
The second failed because it isn't approaching the problem correctly. If you are making the array smaller each time, you have to work backwards. If you remove the object at index 0, then the object that was at index 1 is now at index 0. But your next pass removes the object at index 1, and so on.
I'm a little surprised that the 3rd one worked, because you can't change the size of the array when doing fast enumeration. I'm going to guess that it works here because the subviews property returns a copy of the array, so you aren't messing with the actual subview array.
Somebody posted a cool array trick here a few weeks ago. I haven't tested this code, but it should be something like this:
// Clear up existing scale if we need to by removing any labels int i,j; int totalSubViews = [[self.view subviews] count]; for (i=0,j=0; i < totalSubViews; i++ ) { if ([[[self.view subviews] objectAtIndex:j] isKindOfClass:[UILabel class]]) { UILabel *thisLabel = [[self.view subviews] objectAtIndex:j]; NSLog (@\"Removing %@\",thisLabel.text); [thisLabel removeFromSuperview]; } else j++; }
The first one failed because subviews is not a property of self, which is presumably your view controller. If this was a custom UIView subclass, it would probably work.
The second failed because it isn't approaching the problem correctly. If you are making the array smaller each time, you have to work backwards. If you remove the object at index 0, then the object that was at index 1 is now at index 0. But your next pass removes the object at index 1, and so on.
I'm a little surprised that the 3rd one worked, because you can't change the size of the array when doing fast enumeration. I'm going to guess that it works here because the subviews property returns a copy of the array, so you aren't messing with the actual subview array.
Somebody posted a cool array trick here a few weeks ago. I haven't tested this code, but it should be something like this:
This is the most elegant of the methods posted here. Thanks, Brian.
Elegant is a dirty word. makeObjectsPerformSelector says in the docs that you shouldn't give it something that modifies the array. removeFromSuperview does. Make a copy of the subviews array and work on it and stop trying to write one line solutions. (Same problem as a vanilla For loop affecting the array it is working over.)
Elegant is a dirty word. makeObjectsPerformSelector says in the docs that you shouldn't give it something that modifies the array. removeFromSuperview does. Make a copy of the subviews array and work on it and stop trying to write one line solutions. (Same problem as a vanilla For loop affecting the array it is working over.)
If you had actually read the documentation before posting, then you would know that this is unnecessary, since subviews is a copy property. Plus it is an NSArray, not an NSMutableArray, therefore it cannot be modified. So if this solution didn't work, it would crash very quickly, and this would have been noted here in this thread quite some time ago.
That would make way too much sense, can't be doing that now! :D
So I set up a button to place a view as a subview to self.view. On this subview I put a button that calls the remove from superview for loop.
If the loop would not remove subviews of subviews I should see a leak in instruments but I was presenting and removing the view a good few times and no leak appeared so I think we can safely say that everything is indeed removed.
Replies
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomefor (UIView *view in self.subviews) {[view removeFromSuperview];
}
The above gave me the Warning message of "UIView may not respond to -countByEnumerationWithState:objects:count: And my code crashed when I tried to run it.
I used this and it worked for me:
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThis would not compile for me:
for (UIView *view in self.subviews) {[view removeFromSuperview];
}
And this did not remove all subviews for some reason:
for (int i = 0; i < [[self.view subviews] count]; i++ ) {[[[self.view subviews] objectAtIndex:i] removeFromSuperview];
}
But what did work is as follows:
for (UIView *view in [self.view subviews]) {[view removeFromSuperview];
}
This method compiled and removed all subviews.
Apps for Finance and Apps for Fun
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe second failed because it isn't approaching the problem correctly. If you are making the array smaller each time, you have to work backwards. If you remove the object at index 0, then the object that was at index 1 is now at index 0. But your next pass removes the object at index 1, and so on.
I'm a little surprised that the 3rd one worked, because you can't change the size of the array when doing fast enumeration. I'm going to guess that it works here because the subviews property returns a copy of the array, so you aren't messing with the actual subview array.
Somebody posted a cool array trick here a few weeks ago. I haven't tested this code, but it should be something like this:
SlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomei think that a for cycle may cause some problem with the array varying in count while removing the subviews...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeMake a donation via PayPal.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
1 • Off Topic Insightful Disagree Dislike Like 1Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomefor (UIView *view in [self.view subviews]) {[view removeFromSuperview];
}
:D
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeWill
remove all subviews of subviews or will I have to nest another for loop to get rid of them safely?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSlickShopper 2 | BTIConcepts on GitHub | Free NSLog utility | Free Getter Utility | Leave a PayPal donation.
Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSo I set up a button to place a view as a subview to self.view. On this subview I put a button that calls the remove from superview for loop.
If the loop would not remove subviews of subviews I should see a leak in instruments but I was presenting and removing the view a good few times and no leak appeared so I think we can safely say that everything is indeed removed.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome