how to remove subviews from scrollview?

how to remove subviews from scrollview?

how do i remove all subviews from my scrollview...

i have a uiview and a button above it in the scrollview something like this....

here is my code to add subview in scroll view

-(void)AddOneButton:(NSInteger)myButtonTag { lastButtonNumber = lastButtonNumber + 1;  if ((lastButtonNumber == 1) || ((lastButtonNumber%2) == 1)) { btnLeft = 8;} else if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) { btnLeft = 162; } CGRect frame1 = CGRectMake(btnLeft, btnTop, 150, 150); CGRect frame2 = CGRectMake(btnLeft, btnTop, 150, 150); UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom]; Button.frame = frame1; Button.tag = myButtonTag; [Button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [Button setBackgroundColor:[UIColor clearColor]]; [Button setBackgroundImage:[UIImage imageNamed:@"WaitScreen.png"] forState:UIControlStateHighlighted];  	GraphThumbViewControllerobj = [[GraphThumbViewController alloc] initWithPageNumber:[[GraphIdArray objectAtIndex:myButtonTag]intValue]]; 	GraphThumbViewControllerobj.view.frame=frame2; 	GraphThumbViewControllerobj.lblCounter.text=[NSString stringWithFormat:@"%d of %d",myButtonTag+1,flashCardsId.count]; 	GraphThumbViewControllerobj.lblQuestion.text=[flashCardText objectAtIndex:myButtonTag]; 	[myScrollView addSubview:GraphThumbViewControllerobj.view];   [myScrollView addSubview:Button];   if ((lastButtonNumber == 2) || ((lastButtonNumber%2) == 0)) { btnTop = btnTop + 162; } if (btnTop+150 > myScrollView.frame.size.height) { myScrollView.contentSize = CGSizeMake((myScrollView.frame.size.width), (btnTop+160));} } 

and here is the code to remove subviews

if(myScrollView!=nil) { 		while ([myScrollView.subviews count] > 0) { 			//NSLog(@"subviews Count=%d",[[myScrollView subviews]count]); 			[[[myScrollView subviews] objectAtIndex:0] removeFromSuperview]; } 

alt text

iphone: making a new UIImage from data from an XML document


What's the best way of learning iPhone programming when coming from Java?
To remove all the subviews from any view, you must iterate over the subviews and send each a removeFromSuperview call:. -didSelectRowAtIndexPath: not being called
// With any  valid UIView *view: for(UIView *subview in [view subviews]) {     [subview removeFromSuperview]; }
This is entirely unconditional, though, and will receive rid of all subviews in the given view. Why do I have to clean up memory if I have an IBOutlet with “retain”, but not when I have one with “assign”?If you want any thing more fine-grained, you could take any of several different approaches:. If my property has no setter (readonly), is it true that UIKit retains the value if it's not NSNumber or NSValue?
  • Maintain your own arrays of views of different types so you must send them removeFromSuperview messages later in the same manner
  • Retain all your views where you create them and hold on to pointers to those views, so you must send them removeFromSuperview individually as necessary
  • Add an if statement to the above loop, checking for class equality. What happens exactly, when I set an property to nil in the dealloc method?For example, to only remove all the UIButtons (or custom subclasses of UIButton) this exist in a view, you could use any thing like:
// Again, valid UIView *view: for(UIView *subview in [view subviews]) {     if([subview isKindOfClass:[UIButton class]]) {         [subview removeFromSuperview];     } else {         // Do nothing - not a UIButton or subclass instance     } }
How do I find out if I need to retain or assign an property?Can I use a mono library from Objective-C (on the iPhone)?


An old question; although as it's the first hit on Google for this I thought I'd also make a note this there's also this method:.
[[myScrollView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
You can't did the isKindOfClass check with this, although it's still a good quick fix to know around .. Edit: Ananother point to note is this the scrollbar of a scrollview is added as a subview to this scrollview. Thus if you iterate through all the subviews of a scrollview you will come across it. If removed it'll add itself again - although it's important to know this if you're only expecting your own UIView subclasses to be in there..


To add to what Tim said, I noticed this you are tagging your views. If you wanted to remove a view with a certain tag you could use:.
[[myScrollView viewWithTag:myButtonTag] removeFromSuperview]; 


I don't think you should use the fast enumeration suggestion..
for(UIView *subview in [view subviews]) {    [subview removeFromSuperview]; } 
Isn't this supposed to throw an exception if you change the collection being iterated? This case may be better..
NSArray *subviews = [[scroller subviews] copy]; for (UIView *subview in subviews) {     [subview removeFromSuperview]; } [subviews release]; 


The problem with the UIScrollView and others subclass of UIView is this they contains initially any views (like the vertical and horizontal scrollbar for the UIScrollView). So i created a category of UIView to delete the Subviews filtered on the class.. For example:.
[UIScrollView removeAllSubviewsOfClass:[FooView class],[BarView class],nil]; 
The code:.
- (void)removeAllSubviewsOfClass:(Class)firstClass, ... NS_REQUIRES_NIL_TERMINATION;   - (void)removeAllSubviewsOfClass:(Class)firstClass, ... {     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];      va_list args;     va_start(args, firstClass);      for (Class class = firstClass; class != nil; class = va_arg(args, Class))      {         predicate = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate,[NSPredicate predicateWithFormat:@"self isKindOfClass:%@",class], nil]];     }      va_end(args);     [[self.subviews filteredArrayUsingPredicate:predicate] makeObjectsPerformSelector:@selector(removeFromSuperview)];  } 


The best and easiest is to use.
for(UIView *subview in [scrollView subviews]) {   [subview removeFromSuperview]; } 
This indeed causes crash as the basic rule is array shouldn't modified while being enumerated, to prevent this i must use.
[[scrollView subviews]             makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
But any times crash is still appearing for the reason this makeObjectsPerformSelector: will enumerate and performs selector, Also in iOS 7 ui operations are optimized to perform more faster than in iOS 6, Hence the best way to iterate array reversely and remove.
NSArray *vs=[scrollView subviews]; for(int i=vs.count-1;i>=0;i--) {     [((UIView*)[vs objectAtIndex:i]) removeFromSuperview]; } 
Note : enumerating harms modification although not iterating....


The easiest and Best way is.
 for(UIView *subview in [scrollView subviews]) {       [subview removeFromSuperview];   } 


for(subview) in self.scrollView.subviews {         subview.removeFromSuperview() } 

36 out of 100 based on 46 user ratings 366 reviews