2010-01-06 106 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; 

我希望這是一些幫助的