全部问候,xcode从视图中删除一些子视图
我是一个noob,我一直试图通过这几天工作。
我正在通过UItouch将图像添加到视图。该视图包含添加新图像的背景。如何清除我从子视图中添加的图像,而无需摆脱作为背景的UIImage。任何援助非常感谢。提前致谢。
这里是代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps==2) {
imageCounter.text [email protected]"two taps registered";
//__ remove images
UIView* subview;
while ((subview = [[self.view subviews] lastObject]) != nil)
[subview removeFromSuperview];
return;
}else {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGRect myImageRect = CGRectMake((touchPoint.x -40), (touchPoint.y -45), 80.0f, 90.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"pg6_dog_button.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
[imagesArray addObject:myImage];
NSNumber *arrayCount =[self.view.subviews count];
viewArrayCount.text =[NSString stringWithFormat:@"%d",arrayCount];
imageCount=imageCount++;
imageCounter.text =[NSString stringWithFormat:@"%d",imageCount];
}
}
回答1通常是正确的,但请注意,您可以使用限制搜索[UIView的viewWithTag:7]不必遍历每一个视图。当然,这可能无法为你节省任何东西,但知道 – justin 2010-11-09 22:29:49
是一件很棒的事情,你们都非常感谢你的意见。我今天会试一试。我做了很长时间的工作,其中的一切都是在所有事情都被清除后重新添加背景。不完全优雅,但它的工作.. – 2010-11-10 17:24:26
你的解决方法是非常聪明,但它可能无法很好地工作,当你需要更多的控制你的意见。如果您按照@justin的建议尝试使用'viewWithTag:',请记住它只会返回匹配标签找到的第一个视图。只有一个这样的视图时,这是一个非常方便的方法。如果你有很多匹配标签的视图,你最终会迭代找到它们。额外提示:视图的默认标记值为零,因此请避免使用零作为标识标记值。 – 2010-11-10 18:05:19