简体   繁体   English

iPhone,方法返回具有+1保留计数的Objective-C对象

[英]iPhone, method returns an objective-c object with a +1 retain count

I kind of understand why I'm getting this analyzer warning. 我有点理解为什么收到此分析仪警告。 Because I'm using an object that is being passed in. I've tried autorelease and retain however these cause me other problems like unrecognized selector sent to instance . 因为我使用的是传入的对象,所以尝试了autoreleaseretain这些内容,但是这导致了其他问题,例如unrecognized selector sent to instance

The aim of my CommonUI function is to re-use code, but I have to cater for addSubView and presentModalViewController . 我的CommonUI函数的目的是重用代码,但是我必须迎合addSubViewpresentModalViewController

Perhaps I'm doing some obvious wrong ? 也许我在做一些明显的错误?

替代文字

Change your code like this: 像这样更改代码:

HelpViewController *helpvc = [[HelpViewController alloc] init....];
[vw addSubview:helpvc.view];
[helpcv release];

I think you don't need to pass the other VC. 我认为您不需要通过其他VC。

There are two problems here. 这里有两个问题。

First, if you call [vc release] (as the other answers suggest), you'll certainly make the analyzer happy but likely crash the app. 首先,如果您调用[vc release] (如其他答案所示),则可以使分析仪满意,但可能会使应用程序崩溃。 A view controller's view doesn't retain the controller, so any button targets in the view will be pointing to garbage. 视图控制器的视图不会保留该控制器,因此视图中的任何按钮目标都将指向垃圾。

You will need to somehow keep the HelpViewController retained for as long as it is showing up onscreen. 您需要以某种方式将HelpViewController保留到屏幕上。 The "parent" view controller should likely retain it somehow. “父”视图控制器可能应该以某种方式保留它。 You could autorelease it, and return it. 您可以自动释放它,然后将其返回。 Then whomever calls showHelpClick... would retain the returned controller. 然后,调用showHelpClick...任何人都将保留返回的控制器。

Second, you don't need to have the (UIViewController *)vc passed in as an argument. 其次,您不需要传递(UIViewController *)vc作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM