简体   繁体   中英

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

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

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;

}

then now remove button frame when we want to again start for loop ....in this case if ARRYA.count is 4 then 4 frame set but we want to next time Arrya.count is 2 but last to frame is not remove ....What's the solution?

Ok, this method will remove all the buttons in your scrollview,

Call this method when you want to remove old Buttons in the scrollview.

-(void) removeMyButtons{

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

}

first of all you are re-allocating button release button in for loop like..

IF YOU ARE NOT USING ARC

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

     /// Your Code......

     [Test1 release];
}

then to remove all buttons use this

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

write this code before your for loop in which you add all buttons. that means you remove all button before the adding all buttons.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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