由于某种原因,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 返回的对象是一个图标。 NSLog 2 返回的对象是 UIImageView,尽管我在代码中彻底搜索了每个 UIImageView 并给它一个 -1 标签。通过移动 NSLog 2,我发现 [icon removeFromSuperview];
行是这里的问题。如果不包括该行,则不会发生这种情况。但显然我需要从超级视图中删除它,而 .alpha = 0
是太多的修补修复。
I have been through every single thing allocated in my code and given it a tag of -1, except from the Icons, which use up the tag system. So here's my code:
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 returns the object to be an icon. NSLog 2 returns the object to be a UIImageView, despite me searching my code thoroughly for every UIImageView and giving it a -1 tag. Through moving NSLog 2 around, i've discovered that the line [icon removeFromSuperview];
is the problem here. If that line isn't included, it doesn't happen. But obviously i need to remove it from superview and .alpha = 0
is too much of a patch-over fix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为标签使用什么值?有时,如果我使用的值太低,我就会遇到麻烦(我假设是因为
UIKit
正在使用该标记值)。尝试将
index
设置为某个随机的大数字。另外,为什么不直接使用实例变量来引用图标呢?这样您就不必费力通过标签来识别图标了。
What value are you using for the tag? Sometimes I have troubles if I use a value that's too low (I assume because
UIKit
is using that tag value).Try setting
index
to some random large number.Also, why not just use an instance variable to refer to the Icon? Then you won't have to mess around with identifying the icon by its tag.