2010-01-06 53 views
0

我在我的项目中添加了一个UIScrollView ...我添加了10个按钮“fieldButton”一次,然后我想添加其他5个按钮的同一个UIScrollView。它试图做到这一点第一次添加10按钮也进入scrollview ..如何删除第一个添加10按钮之前添加其他项目。在相同的滚动视图...删除按钮形式滚动视图

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]; 
} 

回答

0

如果你只想清除您的滚动型(即删除其所有子视图),那么你可以做下列方式:

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

如果你想删除一些具体看法,你可以检查它的类型:

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

您还可以标记属性分配给你的意见和删除它们下列方式:

[[fieldButton_area viewWithTag:yourTag] removeFromSuperView]; 

另请注意,您必须在某处释放您的按钮,否则您将发生内存泄漏。

+0

非常感谢....帮助.... – jaleel 2010-01-06 11:32:51

+0

它不能正常工作 – jaleel 2010-01-06 13:59:47

+0

究竟是什么错? – Vladimir 2010-01-06 14:27:14

0

您也可以尝试将按钮的动画设置为离开屏幕或将alpha设置为0,然后将新的UIButton添加到视图。这可能会使用更多的内存,但在某些情况下看起来更好。

[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; 

我希望这是一些帮助的