gpt4 book ai didi

objective-c - 如何从 ScrollView 中删除 subview ?

转载 作者:太空狗 更新时间:2023-10-30 03:08:52 25 4
gpt4 key购买 nike

如何从 ScrollView 中删除所有 subview ...

我在 ScrollView 中有一个 uiview 和它上面的一个按钮,就像这样....

这是我在 ScrollView 中添加 subview 的代码

-(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));}
}

下面是删除 subview 的代码

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

alt text

最佳答案

要从任何 View 中删除所有 subview ,您可以遍历 subview 并向每个 subview 发送一个 removeFromSuperview 调用:

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

不过,这是完全无条件的,并且会删除给定 View 中的所有 subview 。如果您想要更细粒度的东西,您可以采用几种不同的方法中的任何一种:

  • 维护您自己的不同类型的 View 数组,以便您可以稍后以相同的方式向它们发送 removeFromSuperview 消息
  • 在创建它们的地方保留所有 View 并保留指向这些 View 的指针,因此您可以根据需要单独发送它们 removeFromSuperview
  • 在上面的循环中添加一个if 语句,检查类是否相等。例如,要仅删除 View 中存在的所有 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
}
}

关于objective-c - 如何从 ScrollView 中删除 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1310723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com