Delete all objects from UIView

I am using 2 UIView to draw different objects in an xib file. You need to clear the view before drawing new objects for some actions. Initially, when the types of objects are smaller in number, I used this:

for (UILabel *btn in self.contentView.subviews) { if([btn isKindOfClass:[UILabel class]]) { [btn removeFromSuperview]; } } 

But when I have several actions and objects of several types that need to be drawn for each action, it does not code well using this type of method. Is there an effective method for doing this?

+6
source share
2 answers

You must use this to remove all subqueries, regardless of their class.

 [self.contentView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)]; 
+15
source

Use this to remove an entire object from a UIView

  NSArray *arr=view.subviews; //Your Vies object.subviews for (UIView *view1 in arr) { [view1 removeFromSuperview]; } 
0
source

Source: https://habr.com/ru/post/958904/


All Articles