我已經使用IB添加了一個UIScrollVIew。然後使用下面的代碼,我給這個UIScrollView添加幾個按鈕。從UIScrollView中刪除按鈕
for (int index=0; index<3;index++)
{
UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setFrame:CGRectMake(18,200,300,45)];
[button setTag:100+index];
[button addTarget:self action:@selector(butclick:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
}
現在我需要一種方法來刪除我從ScrollView中添加的這些按鈕。
注意:還有其他按鈕,圖像,標籤附加到UIScrollView。那些不應該被刪除。只有按我編程方式添加的按鈕才能從UIScrollView中刪除。
我會接受@安東尼的解決方案,但如果你不確定的按鈕是如何被前面添加,跑他環路到可能存在的最大數量。在某些情況下,viewWithTag會變爲零,但發送[nil removeFromSuperview]沒有任何傷害; – danh 2012-04-06 15:01:48