繁体   English   中英

当我们在for循环内创建Button框架时,现在我们要删除或清理按钮框架,那该怎么办呢?

[英]When we create Button frame within for loop then now we want to remove or clean button frame then how can do this?

当我们在for循环内创建Button框架时,现在我们要删除或清理按钮框架,那该怎么办呢?

for(int i=0 ;i<(self.WebService->ptr1).count ;i++)
{

     Test1=[[UIButton alloc]initWithFrame:CGRectMake(x,y,w,h)];
    [Test1 addTarget:self action:@selector(TestDescription1:)forControlEvents:UIControlEventTouchUpInside];
    Test1.tag=i;
    NSLog(@"test =%d",Test1.tag);
    NSString *s1 =[NSString stringWithFormat:@"%@",[[self.WebService->ptr1 objectAtIndex:i]valueForKey:@"Description"]];
    NSLog(@"s1 =%@",s1);

    NSString *s2 =[NSString stringWithFormat:@"%@",[[self.WebService->ptr1 objectAtIndex:i]valueForKey:@"TestId"]];
    NSLog(@"s2 =%@",s2);
    [TestID addObject:s2];
    NSLog(@"test id=%@",TestID);
    [Test1 setTitle:s1 forState:UIControlStateNormal];
    [scrollview addSubview:Test1];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TestDescription1:)];

    [Test1 addGestureRecognizer:tap];

    Test1.userInteractionEnabled=YES;

    tap.numberOfTapsRequired = 1;

     y=y+40;

}

然后,当我们想再次开始循环时删除按钮框架....在这种情况下,如果ARRYA.count为4,则设置为4帧,但是我们希望下次Arrya.count为2,但最后一个不删除。 ..解决方案是什么?

好的,此方法将删除滚动视图中的所有按钮,

当您要删除滚动视图中的旧Button时,请调用此方法。

-(void) removeMyButtons{

    for(UIView *v in scrollview.subviews){
        if([v isKindOfClass:[UIButton class]]){
            [v removeFromSuperview];
        }
    }

}

首先,您要在for循环中重新分配按钮释放按钮。

如果您不使用ARC

for(int i=0 ;i<(self.WebService->ptr1).count ;i++)
{
     Test1=[[UIButton alloc]initWithFrame:CGRectMake(x,y,w,h)];

     /// Your Code......

     [Test1 release];
}

然后删除所有按钮使用此

-(void) removeAllButtons{
    for(UIButton *btn in scrollview.subviews){
            [btn removeFromSuperview];
    }
}

在您添加所有按钮的for循环之前编写此代码。 这意味着您要先删除所有按钮,然后再添加所有按钮。

for (UIView* subView in scrollview.subviews)  {
    if ([subView isKindOfClass:[UIButton class]])
        [subView removeFromSuperview];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM