我使用循环将多个视图添加到视图中。
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];
}
}
答案 0 :(得分:1)
考虑到您的Button
被直接添加到您正在循环添加的View
,您可以使用
SuperView
行动
-(void) removeFavouriteContact :(id)sender {
[[sender superview] removeFromSuperView];
}
以上代码仅在您的按钮被直接添加到视图中时才有效,如果没有检查添加它的按钮级别,则在深层次中您只需追加superview
并获得如下所示的精确超级视图
[[[[sender superview] superview] superview] removeFromSuperView];
希望它有所帮助。
干杯。
答案 1 :(得分:0)
使用tag
和viewWithTag
属性。为tag
&amp;组view
设置button
view
并使用tag
获取-(void) removeFavouriteContact :(id)sender {
NSInteger tag = sender.tag;
UIView *view = [contactsContainer viewWithTag:tag];
[view removeFromSuperView];
}
,然后将其删除。
{{1}}
答案 2 :(得分:-1)
只使用一行
[[someUIView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];