0

当我们在 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;

}

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

4

3 回答 3

0

首先,您要在 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];
    }
}
于 2013-05-14T12:15:19.007 回答
0

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

当您想删除滚动视图中的旧按钮时调用此方法。

-(void) removeMyButtons{

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

}
于 2013-05-14T12:00:36.453 回答
0

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

for (UIView* subView in scrollview.subviews)  {
    if ([subView isKindOfClass:[UIButton class]])
        [subView removeFromSuperview];
}
于 2013-05-14T12:15:32.713 回答