我如何从滚动视图中删除所有子视图...
我在滚动视图中有一个 uiview 和它上面的一个按钮,像这样......
这是我在滚动视图中添加子视图的代码
-(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
call:
// With some valid UIView *view:
for(UIView *subview in [view subviews]) {
[subview removeFromSuperview];
}
但这是完全无条件的,并且会摆脱all给定视图中的子视图。如果您想要更细粒度的东西,您可以采取以下几种不同方法中的任何一种:
- 维护您自己的不同类型的视图数组,以便您可以发送它们
removeFromSuperview
稍后以同样的方式发送消息
- 保留所有创建视图的位置并保留指向这些视图的指针,以便您可以发送它们
removeFromSuperview
根据需要单独
- Add an
if
上面循环的语句,检查类的相等性。例如,要仅删除视图中存在的所有 UIButton(或 UIButton 的自定义子类),您可以使用类似以下内容的内容:
// 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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)