整个视图控制器中的多个presentViewController方法调用会导致iOS中的内存泄漏吗? [英] Will multiple presentViewController method calls throughout the view controllers lead to memory leak in iOS?
问题描述
我知道已经发生了很多与此主题相关的讨论.但是在所有讨论中,所有讨论都使用2个视图控制器(A& B).我的情况类似但不同.
I know there has been a lot of discussions occurred related to this topic. But in all discussions all have discussed with 2 view controllers (A&B). My scenario is similar but different.
当有多个视图控制器(如A,B,C,D)时会发生什么.因此,呈现流程的移动方向为,
What will happen when there are multiple view controllers like A,B,C,D. So the presenting flow moves as ,
视图控制器A(主页)显示视图控制器B(列表).然后从View Controller B呈现View Controller C(详细信息),然后从View Controller C呈现View Controller D(高级详细信息).然后从View Controller D提出View Controller A,以便直接导航到Home!
View controller A (Home) presents View controller B(List). Then from View controller B presents View Controller C (Details).Then from View Controller C presents View Controller D(Advanced Details). Then from View Controller D presents View Controller A , in order to navigate straight to Home !!!
那是什么最佳做法?
推荐答案
我不知道您的项目结构以及如何显示A,B,C,D,E,F,然后从F返回A的详细信息,我会做出一个大胆的猜测,并说这可能会导致内存泄漏,具体取决于您对UIViewControllers采用的设计模式.正如@CaptJak在您的问题中所评论的那样,很难确定是否,如何以及何时会导致内存泄漏,尤其是如果您使用委托模式在视图控制器周围传递数据.
Not knowing your project structure and details of how you will display A,B,C,D,E,F and then from F back to A, I would take a wild guess and say that it may lead to a memory leak depending on what design patterns you employ to your UIViewControllers. As commented by @CaptJak in your questions, it has hard to tell if, how and when it will cause a memory leak, especially if you use delegation pattern to pass data around view controllers.
就个人而言,当我执行复杂的流程(例如呈现多个UIViewControllers)并发现自己需要返回几个屏幕时,我将不会弹出堆栈上的视图,直到我想要的视图位于堆栈顶部为止(如果您(正在使用导航控制器),如果以模态显示,则关闭视图控制器,或者如果我使用它们,则放开搜索.这里的风险可能是视图控制器的内存可能已释放.
Personally, when I do complicated flows such as presenting multiple UIViewControllers and find myself needing to go back a few screens, I will neither pop the views on the stack up until the one I want is on top of the stack (if you are using navigation controller), dismiss view controller if it is presented modally, or unwind segues if I use them. The risk here might be the view controller's memory may have deallocated.
我本来可以发表评论,但是我没有足够的声誉.用我的话来回答我的问题,因为我在iOS开发中还很新鲜.
I would have commented but I don't have enough reputation. Take my answer with a grain of salt as I am a still quite fresh in iOS development.
感谢您提供应用程序流程中提供的详细信息.由于您可以使用presentViewController,因此我假设您在NavigationController上运行?如果是这种情况,我将在这种情况下使用popToViewController
或popToRootViewController
(如果A是您的根视图控制器),而不是再次从D呈现A.如果再次从D提供A,我想您在VC堆栈中将有2个A实例,这可能会导致内存泄漏.
Thank you for the details provided in your app flow. Since you could use presentViewController, I am assuming you are running on a NavigationController? If that is the case, I would use popToViewController
or popToRootViewController
(if A is your root view controller) for this case instead of presenting A from D again. If A is presented from D again, I am guessing that you will have 2 instances of A in your VC stack which may lead to memory leak.
PopToViewController方法
NSArray arrayOfVCs = self.navigationController.viewControllers;
for(UIViewController *currentVC in arrayOfVCs)
{
if([currentVC isKindOfClass:[ViewControllerA class])
{
[self.navigation.controller popToViewController:currentVC animated:YES]
}
}
PopToRootViewController方法(假设A是导航控制器中的根视图)
[self.navigationController popToRootViewControllerAnimated:YES]
编辑2016年4月12日
我一直在思考这个问题,以至于我实际上对此做了简短的RnD,只想在这里分享我的发现.我做了一个简单而又肮脏的函数,以获取我非常简单的应用程序中曾经展示过的所有模态视图控制器的堆栈.
I have just been thinking about this question so much that I actually did a short RnD on it and just want to share my findings here. I made a crude simple and dirty function to get a stack of all modal view controllers ever presented in my very simple app.
我通过A-> B-> C-> B-> C-> B进行了大约20-30次测试.每次我从B-> C时,内存增加了0.5MB(因为我的屏幕很简单,但您的屏幕可能有所不同),最终从20+ MB增加到50+ MB.
I tested by from A -> B -> C-> B -> C -> B about 20 - 30 times. Everytime I go time B -> C, the memory increased by 0.5MB (because my screens are simple but yours may differ) and ended up increasing from 20+ MB to 50+ MB.
在此函数中,我在堆栈中记录了所显示的视图控制器的数量以及它们在数组中的名称.到目前为止,它对我有用,但是您可以尝试一下,看看它是否对您有用.
In this function I recorded the number of presented view controllers in the stack and also their names in an array. So far it worked for me but you can give it a try and see if it works for you.
//global variables
var vccount = 0
var vcnamelist = [String]()
func getPresentingViewStackCount(currentVC : UIViewController!){
if(currentVC.presentingViewController != nil){
vccount = vccount + 1
let vc = currentVC.presentingViewController
vcnamelist.append(NSStringFromClass((vc?.classForCoder)!))
getPresentingViewStackCount(vc)
}
}
// to use
func someRandomMethod(){
getPresentingViewStackCount(self)
}
尽管如此,仍然认为最好使用导航控制器.这就是它们的用途. :)
Still think its best to use navigation controllers though. It's what they are built for. :)
这篇关于整个视图控制器中的多个presentViewController方法调用会导致iOS中的内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!