removeFromSuperview由于某种原因用UIImageView替换对象?

问题描述

| 我已经遍历了代码中分配的每件事,并给它赋予了-1的标记,但图标用完的图标除外。所以这是我的代码
NSLog(@\" 1: %@\",(Icon *)[self viewWithTag:index]);
    Icon *icon = (Icon *)[self viewWithTag:index];
    CGRect frame = icon.frame;
    [icon removeFromSuperview];
    icon = nil;

    Icon *icon2 = [[Icon alloc] init];
    [icon2 makeIconStandardWithTag:(int)index];
    icon2.frame = frame;
    [self addSubview:icon2];

    NSLog(@\" 2: %@\",(Icon *)[self viewWithTag:index]);
NSLog 1将对象返回为图标。尽管我彻底在代码搜索每个UIImageView并将其赋予-1标签,但NSLog 2返回的对象为UIImageView。通过移动NSLog 2,我发现这里的问题是“ѭ1”行。如果不包括该行,则不会发生。但是很明显,我需要将其从Superview中删除,而ѭ2则是一个过度的修补程序。     

解决方法

您为标签使用什么值?如果我使用的值太低,有时会遇到麻烦(我猜是因为
UIKit
正在使用该标记值)。 尝试将
index
设置为一些随机的大数。 另外,为什么不仅仅使用实例变量来引用图标呢?这样,您就不必再通过其标签来识别图标了。