I am creating a UIView every time I click on the button, but the problem is all other views are not getting deleted. It's increasing the memory of the application
Asked
Active
Viewed 587 times
-1
-
2Possible duplicate of [What is the best way to remove all subviews from you self.view?](http://stackoverflow.com/questions/11889243/what-is-the-best-way-to-remove-all-subviews-from-you-self-view) – Yagnesh Dobariya Feb 23 '16 at 06:28
-
NSArray * allSubviews = [self.reletedViewOffer subviews]; for(UIView *view in allSubviews) { if([view isMemberOfClass:[UIButton class]]) { [view removeFromSuperview]; } } – Sudheer Kolasani Feb 23 '16 at 06:29
3 Answers
3
You can set a tag for UIView
objects.
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.tag = 2016;
[self addSubview:view];
Then, you can remove it later using this code :
UIView *view = [self viewWithTag:2016];
[view removeFromSuperview];
You can also keep a reference to an UIView
object with a property.
@property (nonatomic, strong) UIView *view;
So you can remove it very easy.

Michaël Azevedo
- 3,874
- 7
- 31
- 45

wei chen
- 91
- 2
0
Try this:
Assign a tag(may be 100) to the button.
NSArray *subviews = self.view.subviews;
for(UIView *subview in subviews) {
if(subview.tag != 100) {
[view removeFromSuperview];
}
}

TechBee
- 1,897
- 4
- 22
- 46
0
-removeFromSuperview method release's memory after it is called only in case if your view is not retained by anything else
e.g.
Simply removing view from superview may not be sufficient to deallocate it can have view that has an outlet connection & declared property for it with retain or strong attribute, so in this case it will be retained by the controller while it is being loaded from nib file and you may need to release that view.
[yourView removeFromSuperview];
self.yourView = nil;

JasPat88
- 1
- 3