如何从 ios 中有多个子视图的超级视图中删除特定子视图
how to remove a particular subview from superview which has multiple subviews in 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);
}
现在只要我点击按钮。该操作应删除特定的子视图,我 clicked.I 的意思是代码是什么?请帮助我。
-(void) removeFavouriteContact: (UIButton *) sender
{
for(UIView *subview in self.contactsScroller.subviews)
if (self.contactsScroller.subviews)
{
//<#statements#>
//[self.view removeFromSuperview];
}
}
只使用一行
[[someUIView 子视图]
makeObjectsPerformSelector:@selector(removeFromSuperview)];
使用 tag
和 viewWithTag
属性。为您的 view
和 button
设置 tag
并使用 tag
获取 view
然后删除它。
-(void) removeFavouriteContact :(id)sender {
NSInteger tag = sender.tag;
UIView *view = [contactsContainer viewWithTag:tag];
[view removeFromSuperView];
}
考虑到您的 Button
直接添加到您在循环中添加的 View
,您可以使用
轻松访问 SuperView
in you action
-(void) removeFavouriteContact :(id)sender {
[[sender superview] removeFromSuperView];
}
上面的代码只有当你的按钮直接添加到视图时才有效,如果不检查添加按钮的级别,在深层你可以只附加 superview
并获得确切的超级视图如下图
[[[[sender superview] superview] superview] removeFromSuperView];
希望对您有所帮助。
干杯。
我正在使用循环将多个视图添加到一个视图中。
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);
}
现在只要我点击按钮。该操作应删除特定的子视图,我 clicked.I 的意思是代码是什么?请帮助我。
-(void) removeFavouriteContact: (UIButton *) sender
{
for(UIView *subview in self.contactsScroller.subviews)
if (self.contactsScroller.subviews)
{
//<#statements#>
//[self.view removeFromSuperview];
}
}
只使用一行
[[someUIView 子视图] makeObjectsPerformSelector:@selector(removeFromSuperview)];
使用 tag
和 viewWithTag
属性。为您的 view
和 button
设置 tag
并使用 tag
获取 view
然后删除它。
-(void) removeFavouriteContact :(id)sender {
NSInteger tag = sender.tag;
UIView *view = [contactsContainer viewWithTag:tag];
[view removeFromSuperView];
}
考虑到您的 Button
直接添加到您在循环中添加的 View
,您可以使用
SuperView
in you action
-(void) removeFavouriteContact :(id)sender {
[[sender superview] removeFromSuperView];
}
上面的代码只有当你的按钮直接添加到视图时才有效,如果不检查添加按钮的级别,在深层你可以只附加 superview
并获得确切的超级视图如下图
[[[[sender superview] superview] superview] removeFromSuperView];
希望对您有所帮助。
干杯。