如何正确丢弃子视图?

发布于 2024-11-09 16:56:41 字数 751 浏览 0 评论 0原文

我的应用程序有一个问题,其中的代码太长而无法进入,但足以说明当我删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三人与歌 2024-11-16 16:56:41

“有没有办法彻底删除
图标,或者是到目前为止的removeFromSuperview
我能走就走”

你可以将对象设置为零:

icon = nil;

"Is there a way to completely remove
icon, or is removeFromSuperview as far
as i can go"

You can set the object to nil:

icon = nil;
薄荷梦 2024-11-16 16:56:41

你能验证一下这行代码中的“self”是什么吗:
可能不是你想的那样。

  [self addSubview:icon2];
  NSLog(@" Self is %@", self);

Can you verify what "self" is in this line of code:
It might not be what you think.

  [self addSubview:icon2];
  NSLog(@" Self is %@", self);
清欢 2024-11-16 16:56:41

这是一个猜测,但请尝试将 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 your Icon objects. The viewWithTag: method searches the current view and its subviews for a match, so if self.tag == 0 and you call [self viewWithTag:0], you'll get self.

花期渐远 2024-11-16 16:56:41

在此之前您是否在某处保留了图标?如果不是,则调用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文