当存在视图控制器时,使用 ARC 的正确方法是什么?

发布于 2025-01-03 20:37:29 字数 410 浏览 2 评论 0原文

每次我展示我的新视图控制器(作为演示者 vc 中的属性)时,如下所示:

if(self.viewController) [self.viewController release];
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

所以我总是保证获得一个新的对象实例(如果该对象已经存在)。

但现在我使用 ARC 并且不知道如何发送我的对象释放消息(因为不适用于 ARC)

你能帮我解决这个问题吗?

PS:每次我展示 gameVC 时,我都需要该对象的一个​​新实例。

谢谢大家!

Every time I present my new viewcontroller (as property in presenter vc) as show below:

if(self.viewController) [self.viewController release];
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

so I'm always guaranteed to get a new instance of object (if the object already exists).

But now I use ARC and don't have any ideas how to send my object release message (because is not work with ARC)

Can you help me with this issues?

PS: Every time when I present gameVC I need an new instance of the object.

Thanks all!

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

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

发布评论

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

评论(4

圈圈圆圆圈圈 2025-01-10 20:37:29
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

上面的代码将始终呈现一个新的 vc,苹果的编译器将负责发布。

但是如果你认为上面有一些问题(这是一个神话)那么你也可以这样做

self.viewController = nil;
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

the above code will always presents a new vc and apple's compiler will take care of the release.

But if you think the above have some problem(which is a myth) then you can do also like this

self.viewController = nil;
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];
梦屿孤独相伴 2025-01-10 20:37:29

编译器为你放​​置了释放指令。您应该仅使用此代码并且它应该可以正常工作:

self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

The compiler puts the release instruction for you. You should use only this code and it should work OK:

self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];
南风几经秋 2025-01-10 20:37:29

您不应该持有指向视图控制器的指针。
我很确定你的代码应该如下所示:

ViewController *viewController = [[ViewController alloc] init];
[self presentModalViewController:viewController animated:YES];

You should not hold a pointer to your view controller.
I'm pretty sure your code should look like this:

ViewController *viewController = [[ViewController alloc] init];
[self presentModalViewController:viewController animated:YES];
澜川若宁 2025-01-10 20:37:29
self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

是正确的方法。

但是,即使您使用手动引用计数,假设 viewController 是具有 retainself 属性,此代码也是可以的>强标志。当你改变属性值时,之前的属性值会被释放,所以你不需要自己释放它。

self.viewController = [[ViewController alloc] init];
[self presentModalViewController:self.viewController animated:YES];

is the right way.

However, this code is OK even when you're using Manual Reference Counting, assuming that viewController is a property of self with retain or strong flag. When you're changing the property value, previous property value gets released, so you don't need to release it by yourself.

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