xcode从视图中删除一些子视图 [英] xcode Removing Some Subviews from view

查看:177
本文介绍了xcode从视图中删除一些子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有

我是菜鸟,而且我已经尝试了几天.

I am a noob and I have been trying to work through this for a few days.

我正在通过UItouch将图像添加到视图中.该视图包含一个背景,在该背景上添加了新图像.如何清除要从子视图添加的图像,而又不摆脱作为背景的UIImage.非常感谢您的协助.预先感谢.

I am adding images to a view via UItouch. The view contains a background on top of which the new images are add. How do I clear the images I am adding from the subview, without getting rid of the UIImage that is the background. Any assistance is greatly appreciated. Thanks in Advance.

下面是代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event { 
NSUInteger numTaps = [[touches anyObject] tapCount];

if (numTaps==2) {
    imageCounter.text =@"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];

}

}

推荐答案

您需要的是一种将添加的UIImageView对象与背景UIImageView区别开来的方法.我可以想到两种方法来做到这一点.

What you need is a way of distinguishing the added UIImageView objects from the background UIImageView. There are two ways I can think of to do this.

方法1:为添加的UIImageView对象分配特殊的标记值

Approach 1: Assign added UIImageView objects a special tag value

每个UIView对象都有一个tag属性,该属性只是一个整数值,可用于标识该视图.您可以像这样将每个添加的视图的标记值设置为7:

Each UIView object has a tag property which is simply an integer value that can be used to identify that view. You could set the tag value of each added view to 7 like this:

myImage.tag = 7;

然后,要删除添加的视图,可以逐步浏览所有子视图,而仅删除标记值为7的子视图:

Then, to remove the added views, you could step through all of the subviews and only remove the ones with a tag value of 7:

for (UIView *subview in [self.view subviews]) {
    if (subview.tag == 7) {
        [subview removeFromSuperview];
    }
}

方法2:记住背景视图

另一种方法是保留对背景视图的引用,以便您可以将其与添加的视图区分开.为背景UIImageView创建一个IBOutlet,并在Interface Builder中以常规方式分配它.然后,在删除子视图之前,只需确保它不是背景视图即可.

Another approach is to keep a reference to the background view so you can distinguish it from the added views. Make an IBOutlet for the background UIImageView and assign it the usual way in Interface Builder. Then, before removing a subview, just make sure it's not the background view.

for (UIView *subview in [self.view subviews]) {
    if (subview != self.backgroundImageView) {
        [subview removeFromSuperview];
    }
}

这篇关于xcode从视图中删除一些子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆