0

for ループ内でボタン フレームを作成するときに、ボタン フレームを削除またはクリーンアップするにはどうすればよいでしょうか。

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

すべてのボタンを追加する for ループの前にこのコードを記述します。つまり、すべてのボタンを追加する前に、すべてのボタンを削除します。

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