从超级视图中删除时 EXC_BAD_ACCESS

发布于 2024-12-17 17:43:41 字数 360 浏览 5 评论 0原文

我的项目使用ARC,所以我不能使用保留或释放,在ViewController A中,我初始化ViewController B并将其视图添加为子视图:

ViewControllerB *viewB = [[ViewControllerB alloc] init];

[self.view addSubview:viewB.view];

在ViewControllerB中,我制作一个按钮,当用户单击它时,视图将被删除来自超级视图:

[self.view removeFromSuperview];

结果是 EXC_BAD_ACCESS。 请帮助我,对我的英语感到抱歉。

My project using ARC, so I can't use retain nor release, in ViewController A, I init ViewController B and add its view as a subview:

ViewControllerB *viewB = [[ViewControllerB alloc] init];

[self.view addSubview:viewB.view];

And in ViewControllerB, I make a button and when user click on it, the view will removed from superview:

[self.view removeFromSuperview];

And the result is EXC_BAD_ACCESS.
Please help me and sorry about my English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花开雨落又逢春i 2024-12-24 17:43:41

我认为 viewB 不是局部变量。

I think viewB is not local variable.

谈场末日恋爱 2024-12-24 17:43:41

我也有同样的问题..

尝试这样的事情:

NSLog("self.view retain count: %d", self.view.retainCount);
[self.view removeFromSuperview];
NSLog("self.view retain count: %d", self.view.retainCount);

或者:

NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);
[self.view removeFromSuperview];
NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);

这不是一个好的做法,但也许你会发现内存泄漏错误...
如果您使用 ARC - 它不能保护您免受内存管理错误的影响...

如果出现问题,我很抱歉我的英语。

I have the same problem too..

Try something like this:

NSLog("self.view retain count: %d", self.view.retainCount);
[self.view removeFromSuperview];
NSLog("self.view retain count: %d", self.view.retainCount);

or:

NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);
[self.view removeFromSuperview];
NSLog("self.view.superview retain count: %d", self.view.superview.retainCount);

It's not good practice, but maybe you will catch a memory leak error...
If you are using ARC - it doesn't protect you from memory management errors...

I'm sorry for my English if something wrong.

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