poptorootviewcontroller不按顺序调用rootview控制器的dealloc方法和viewdidappear方法 [英] poptorootviewcontroller not calling dealloc methods and viewdidappear method of rootview controller in a sequence
问题描述
我有一个带有4个VC的导航控制器.我有一个具有委托属性的单例类,该属性设置为堆栈顶部的VC.我在每个VC的dealloc方法中将此委托设置为nil.我在rootVC的viewdidappear方法中设置委托.
I have a navigation controller with 4 VCs pushed into it. I have a singleton class with a delegate property that is set to the VC which is on top of the stack. I am setting this delegate to nil in the dealloc method of each VC. I am setting the delegate in the viewdidappear method of the rootVC.
当我从第四个VC弹出根VC时,调用dealloc方法(堆栈中所有VC中的所有VC)和viewdidappear方法的顺序如下:
When I pop back to root VC from the 4th VC, the sequence of calling the dealloc methods (of all the VCs in stack) and viewdidappear method is following:
"FirstVC dealloc called"
"SecondVC dealloc called"
"viewdidappear of root VC is called"
"ThirdVC dealloc called"
现在,我面临的问题是,即使我在根VC的viewdidappear方法(也可以从控制流中看到)中将其设置为self,该委托也被设置为nil.如何预防这种情况?我希望所有的VC都真正释放后才能调用viewdidappear方法.
Now, the issue I am facing is that the delegate gets set to nil even though I am setting it to self in the root VC's viewdidappear method (which is visible from the control flow too). How can I prevent this situation? I want the viewdidappear method to get called once all the VCs are really deallocated.
谢谢,欧拜德
推荐答案
dealloc
.当您弹出 ThirdVC
时,由于委托属性仍然保留,因此不会调用 dealloc
.然后,当您的rootVC的 viewDidAppear
被调用时,它将 rootVC
设置为委托.在这一刻,不再需要您的ThirdVC,它会触发 dealloc
.
dealloc
is called automatically once a object is no longer needed. When you pop the ThirdVC
, since the delegate property is still retaining it, dealloc
doesn't get called. Then, when your rootVC's viewDidAppear
gets called, it sets the rootVC
as the delegate. At this momment, your thirdVC is no longer needed, which triggers the dealloc
.
您可以做的一件事是将委托属性设置为nil,而不是在dealloc上,而不是在每个 ViewController
的 viewWillDisappear
方法上,因为肯定会在调用此方法之前接下来的 ViewController
出现.
One thing you could do is set the delegate property to nil not on dealloc, but on the viewWillDisappear
method of each ViewController
, since this method will surely get called before the next ViewController
appears.
这篇关于poptorootviewcontroller不按顺序调用rootview控制器的dealloc方法和viewdidappear方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!