从ScrollView中删除标记对象

时间:2012-07-08 20:04:30

标签: objective-c ios xcode

我在scrollView中有很多图像,所有图像都是一个接一个,还有一个删除按钮作为每个图像的子视图。我以这种方式分配标签

1) img.tag = i; 

2) int z;
        z = i+1;
     buttonDelete.tag = z;

我是for循环。当我按下按钮时,执行此代码:

-(IBAction)deleteMessage:(id)sender
{
    UIButton *button = (UIButton*) sender;
    NSLog(@"Clicked button %d", button.tag); 

    UIImageView *image = (UIImageView*) sender;
    NSLog(@"Clicked button %d", image.tag);

    int n;
    n = button.tag;

    for (UIImageView *img in imageArray)
    {
        if (img.tag == n)
        {
            [img removeFromSuperview];

            NSLog(@"The value of integer num is %i", img.tag);
            NSLog(@"The value of integer num is %i", n);
        }
    }
}

根据NSLog,前四个按钮与其标签和图像标签完全对应。相应的图像被删除。这四个图像都适合滚动视图的第一个视图。如果有更多图像,我需要向下滚动,标签不再增加。相反,它们从标签1重新启动。因此1,2,3,4,1,2,3,4 ...和1,2,3,4可以在滚动视图的同一“页面”中看到。这怎么可能?

编辑:

我也试过这个:

-(IBAction)deleteMessage:(id)sender
{
    UIButton *button = (UIButton*) sender;
    NSLog(@"Clicked button %d", button.tag); 

    UIImageView *image = (UIImageView*) sender;
    NSLog(@"Clicked button %d", image.tag);

    int n;
    n = button.tag;

    for (UIImageView *img in imageArray)
    {
        if ([img isKindOfClass:[UIImageView class]] && img.tag == n) {

            [img removeFromSuperview];
        }
    }
}

但给出相同的结果

0 个答案:

没有答案