开发者

remove button form scrollview

I added one UIScrollView in my project... and I added 10 more button "fieldButton" once, then the same UIScrollView I want add other 5 button. it I try to do this first added 10 button also coming in the scrollview .. how to remove first added 10 button before adding other item. in the same scrollview...

if(a == 1){
    for(int i = 0; i< 10; i++){
        UIButton *fieldButton_area = [[Mos_component alloc]getComboButton:title       andFrame:CGRectMake(0, y, 180, 40)];

        [fieldButton_area addSubview:cid];
        [fieldButton_area addTarget:self action:@selector(get_Area:)  forControlEvents:UIControlEventTouchUpInside];
        [state_scroll addSubview:fieldButton_area];
   }
}
else{
    UIButton *开发者_StackOverflow社区fieldButton_state = [[Mos_component alloc]getComboButton:title andFrame:CGRectMake(0, y, 180, 40)];
    [fieldButton_state addSubview:cid];
    [fieldButton_state addTarget:self action:@selector(get_Area:)  forControlEvents:UIControlEventTouchUpInside];
    [state_scroll addSubview:fieldButton_state];
}


If you want just to clear your scrollview (i.e. remove all its subviews) then you can do that following way:

for (UIView* subView in [state_scroll subviews])
    [subView removeFromSuperView];

If you want to remove some specific views you can check its types:

for (UIView* subView in [state_scroll subviews])
    if ([subView isKindOfClass:[Mos_component class]]) // remove Mos_components only
        [subView removeFromSuperView];

You can also assign tag property to you views and remove them following way:

 [[fieldButton_area viewWithTag:yourTag] removeFromSuperView];

Also note that you must release your buttons somewhere or you will get memory leak.


You could also try animating the button(s) off of the screen or set the alpha's to 0 then add the new UIButton's to the view. This may use more memory, but in some cases look better.

[UIView BeginAnimation];
// set time and speed here

// Perform animation here

[UIView setAnimationDidStopSelector /* here call the method for the new button's animation into the view*/];

[UIView CommitAnimations];
// Then set the button's enabled property to NO
button.enabled = NO;

I hope this is of some assistance

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜