我正在使用循環向視圖添加多個視圖。如何從ios中包含多個子視圖的超級視圖中刪除特定的子視圖
float contactsContainerX = 6.0f;
UIView *contactsContainer=[[UIView alloc] initWithFrame:CGRectMake(0.0f,4.0f,110.0f,22.0f)];
contactsContainer.backgroundColor=[UIColor colorWithRed:213.0f/255.0f green:213.0f/255.0f blue:213.0f/255.0f alpha:1.0f];
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(90.0f, 2.0f, 18.0f, 18.0f)];
closeButton.backgroundColor = [UIColor yellowColor];
[closeButton addTarget:self action:@selector(removeFavouriteContact:) forControlEvents:UIControlEventTouchUpInside];
[contactsContainer addSubview:closeButton];
for(int x=0; x<3;x++)
{
contactsContainer.frame = CGRectMake(contactsContainerX, contactsContainer.frame.origin.y, contactsContainer.frame.size.width, contactsContainer.frame.size.height);
[self.contactsViewSuperContainers addSubview:contactsContainer];
contactsContainerX = contactsContainer.frame.origin.x + contactsContainer.frame.size.width+6.0f;
self.contactsScroller.contentSize =CGSizeMake(contactsContainerX,self.contactsViewSuperContainers.frame.size.height);
}
現在,只要我點擊按鈕。該行動應該刪除我點擊的特定子視圖。我的意思是代碼是什麼?請幫幫我。
-(void) removeFavouriteContact: (UIButton *) sender
{
for(UIView *subview in self.contactsScroller.subviews)
if (self.contactsScroller.subviews)
{
//<#statements#>
//[self.view removeFromSuperview];
}
}
[[sender superview] removeFromSuperview]; – Astoria
您的第一個代碼塊看起來不正確。在你的循環內,你每次都添加相同的視圖。視圖只能添加到另一個視圖一次。 – rmaddy