views:

66

answers:

2

hi friends...

i added one UIScrollView in my project... and i added 10 more button "fieldButton" once, then the same UIScrollView i want add ather 5 button. it i try to do this first added 10 button also coming in the scrollview .. how to remove first added 10 button befor 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 *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];
}

pls help me

A: 

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.

Vladimir
thanks alot.... for helping....
jaleel
it is not working properly
jaleel
what exactly is wrong?
Vladimir
UIView is the problem..can we use here Nssarry instead of UIview ?. then remove each one..tr ?
jaleel
now it is working ... fine ... thank you very much dear Vladimir.i have one doubt on this for loop. and how to access subView the UIButton object... pls helpthen.. i removed it take it out to one nsarray then remove one by one
jaleel
A: 

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

Jaba