how to remove all subviews from my scrollview

To remove all the subviews from any view, you can iterate over the subviews and send each aremoveFromSuperview call:

// With some valid UIView *view:
for(UIView *subview in [view subviews]) {
   
[subview removeFromSuperview];
}

This is entirely unconditional, though, and will get rid of all subviews in the given view. If you want something more fine-grained, you could take any of several different approaches:

  • Maintain your own arrays of views of different types so you can send them removeFromSuperviewmessages later in the same manner
  • Retain all your views where you create them and hold on to pointers to those views, so you can send them removeFromSuperview individually as necessary
  • Add an if statement to the above loop, checking for class equality. For example, to only remove all the UIButtons (or custom subclasses of UIButton) that exist in a view, you could use something 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
   
}
}



If you wanted to remove a view with a certain tag you could use:

[[myScrollView viewWithTag:myButtonTag] removeFromSuperview];




發佈了45 篇原創文章 · 獲贊 1 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章