问题描述
如何从我的滚动视图中删除所有子视图...
how do i remove all subviews from my scrollview...
我在滚动视图中有一个 uiview 和它上方的按钮,如下所示....
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));} }
这是删除子视图的代码
if(myscrollview!=nil) { while ([myscrollview.subviews count] > 0) { //nslog(@"subviews count=%d",[[myscrollview subviews]count]); [[[myscrollview subviews] objectatindex:0] removefromsuperview]; }
推荐答案
要从任何视图中删除所有子视图,您可以遍历子视图并为每个子视图发送 removefromsuperview 调用:
to remove all the subviews from any view, you can iterate over the subviews and send each a removefromsuperview 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:
- 维护您自己的不同类型的视图数组,以便稍后以相同的方式向它们发送 removefromsuperview 消息
- 保留您创建它们的所有视图并保留指向这些视图的指针,以便您可以根据需要单独发送它们 removefromsuperview
- 在上述循环中添加 if 语句,检查类是否相等.例如,要仅删除视图中存在的所有 uibutton(或 uibutton 的自定义子类),您可以使用以下内容:
- maintain your own arrays of views of different types so you can 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 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 } }