当存在视图控制器时,使用 ARC 的正确方法是什么?
每次我展示我的新视图控制器(作为演示者 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
上面的代码将始终呈现一个新的 vc,苹果的编译器将负责发布。
但是如果你认为上面有一些问题(这是一个神话)那么你也可以这样做
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
编译器为你放置了释放指令。您应该仅使用此代码并且它应该可以正常工作:
The compiler puts the release instruction for you. You should use only this code and it should work OK:
您不应该持有指向视图控制器的指针。
我很确定你的代码应该如下所示:
You should not hold a pointer to your view controller.
I'm pretty sure your code should look like this:
是正确的方法。
但是,即使您使用手动引用计数,假设
viewController
是具有retain
或的
标志。当你改变属性值时,之前的属性值会被释放,所以你不需要自己释放它。self
属性,此代码也是可以的>强is the right way.
However, this code is OK even when you're using Manual Reference Counting, assuming that
viewController
is a property ofself
withretain
orstrong
flag. When you're changing the property value, previous property value gets released, so you don't need to release it by yourself.