[英]Remove from superview
Just a small question i'm really having a lot of problems with 只是一个小问题,我真的有很多问题
Basically, what I'm doing is making a view every time I hit a button which works fine. 基本上,我正在做的就是每次按下一个能正常工作的按钮时进行查看。 When I want to remove all the images I made when I hit the remove from superview it just removes the last one on the stack. 当我想删除我从超级视图中删除时创建的所有图像时,它只会删除堆栈中的最后一个。 Is there a way i can get rid of all the images I made? 有什么方法可以摆脱我制作的所有图像吗?
Here is the code 这是代码
This puts the picture on the screen 这会将图片显示在屏幕上
- (IBAction)pushBn:(id)sender {
ZeldaView *newZelda = [[ZeldaView alloc]initWithNibName:@"ZeldaView" bundle:nil];
theZeldaView=newZelda;
[self.view insertSubview:theZeldaView.view atIndex:1];
}
this removes it when i touch it 这会在我触摸时将其删除
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[touches anyObject];
CGPoint location=[touch locationInView:theZeldaView.view];
if (CGRectContainsPoint(theZeldaView.theImage.frame, location)) {
[theZeldaView.view removeFromSuperview];
}
}
Sure, just iterate through the children of your parent view (self.view) and remove any that are ZeldaView elements. 当然,只需遍历父视图(self.view)的子级并删除任何ZeldaView元素即可。
At it's simplest this would be something like: 最简单的说是:
for (UIView* subView in [self.view.subviews])
{
if ([subView isKindOfClass:[ZeldaView class]])
[subView removeFromSuperview];
}
Though you will probably want to expand on this to not perform the actual removal during the iteration and you may want to use respondsToSelector and a custom method instead of checking the class here so that you can do any cleanup needed from within the ZeldaView class. 尽管您可能希望对此进行扩展,以使其在迭代过程中不执行实际的删除操作,但您可能希望使用responsesToSelector和自定义方法,而不是在此处检查该类,以便您可以在ZeldaView类中进行所需的任何清理。 Hopefully that makes sense to you. 希望这对您有意义。
sure, many ways to do this. 当然,有很多方法可以做到这一点。 I would keep an array of ZeldaView
objects and when you want to remove them, traverse the array and remove them all. 我会保留一个ZeldaView
对象数组,当您要删除它们时,遍历该数组并将其全部删除。
in your .h: 在您的.h中:
@property (nonatomic, retain) NSMutableArray *zeldaViews;
when you add a ZeldaView: 当您添加ZeldaView时:
// create a newZeldaView and add it to the superview
[self.zeldaViews addObject:newZeldaView];
when you want to remove them all: 当您想全部删除它们时:
for (ZeldaView *zeldaView in self.zeldaViews) {
[zeldaView.view removeFromSuperview];
}
[self.zeldaViews removeAllObjects];
create the mutable array in viewDidLoad and release it in viewDidUnload and dealloc. 在viewDidLoad中创建可变数组,并在viewDidUnload和dealloc中释放它。 Modify as approp if your using ARC. 如果使用ARC,则修改为适当。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.