如何正确丢弃子视图?
我的应用程序有一个问题,其中的代码太长而无法进入,但足以说明当我删除 UIView 并将其替换为新的 UIView 时,如下所示:
NSLog(@" .. %@", (Icon *)[self viewWithTag:index]);
Icon *icon = (Icon *)[self viewWithTag:index];
CGRect frame = icon.frame;
int tag = icon.tag;
[icon removeFromSuperview];
[icon release];
Icon *icon2 = [[Icon alloc] init];
icon2.frame = frame;
[icon2 makeIconStandardWithTag:(int)tag];
[self addSubview:icon2];
它做了一些奇怪的事情,其中 NSLog第一次(因为视图已经存在)显示该对象是一个图标,但是运行此代码后第二次显示由于某种原因它现在是一个 UIImageView,并且它以某种奇怪的方式显示我认为是原始图标的内容屏幕上的位置。这是非常不稳定的行为。但我所知道的是:
删除[图标removeFromSuperview];行,虽然将对象保留在那里,但会停止此行为并导致 NSLog 返回一个图标,正如它应该的那样。
所以我的猜测是它没有正确删除图标。有没有办法完全删除图标,或者是我所能做到的removeFromSuperview。我能做的就是将其设置为 alpha = 0 但这更多的是一个修补解决方案,而不是我想要的解决方案。
I have a problem with my app where the code for which is far too long to go into, but suffice to say when i'm removing a UIView and replacing it with a new one like so:
NSLog(@" .. %@", (Icon *)[self viewWithTag:index]);
Icon *icon = (Icon *)[self viewWithTag:index];
CGRect frame = icon.frame;
int tag = icon.tag;
[icon removeFromSuperview];
[icon release];
Icon *icon2 = [[Icon alloc] init];
icon2.frame = frame;
[icon2 makeIconStandardWithTag:(int)tag];
[self addSubview:icon2];
It does some weird thing where that NSLog the first time (because the view is already there) shows that the object is an icon, but the second time after running this code shows that it's a UIImageView for some reason now, and it displays what i presume to be the original icon at some odd position on the screen. It's very erratic behaviour. But what i do know is this:
Removing the [icon removeFromSuperview]; line, although keeping the object there, stops this behaviour and causes the NSLog to return an Icon, as it should.
So my guess is that it's not removing icon correctly. Is there a way to completely remove icon, or is removeFromSuperview as far as i can go. What i could do is just have it set to alpha = 0 but this is more of a patch-over solution and not how i want to solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以将对象设置为零:
You can set the object to nil:
你能验证一下这行代码中的“self”是什么吗:
可能不是你想的那样。
Can you verify what "self" is in this line of code:
It might not be what you think.
这是一个猜测,但请尝试将
self.tag
设置为 -1 或其他不会与您在Icon
对象上设置的标签冲突的值。viewWithTag:
方法在当前视图及其子视图中搜索匹配项,因此如果self.tag == 0
并且您调用[self viewWithTag:0]< /code>,你会得到
self
。This is a guess, but try setting
self.tag
to -1 or some other value that doesn't collide with the tags you're setting on yourIcon
objects. TheviewWithTag:
method searches the current view and its subviews for a match, so ifself.tag == 0
and you call[self viewWithTag:0]
, you'll getself
.在此之前您是否在某处保留了图标?如果不是,则调用removeFromSuperview后无需释放它。同样,除非您需要在其他地方引用 icon2,否则您可以在调用 addSubview 后释放它。
视图保留通过addSubview添加的视图,并释放通过removeFromSuperview删除的视图。
Did you retain icon somewhere prior to this? If not, no need to release it after the call to removeFromSuperview. Similarly, unless you need the reference to icon2 elsewhere, you can release that after calling addSubview.
Views retain views added via addSubview, and they release views removed via removeFromSuperview.