问题描述
我今天正在对Iphone应用程序进行一些测试和调试,遇到了一些奇怪的行为.
I was doing some testing and debugging of my Iphone app today and encountered some strange behaviour.
在我的 MainView
中,我有两个模式视图,当使用 presentModalViewController
显示其中之一时,我注意到我的
在显示另一个时未发生时被调用.关闭子视图时, viewDidUnload
方法> MainView viewDidLoad
方法(属于 MainView
)也是相同的情况.
In my MainView
I have two modal views and when showing one of these using presentModalViewController
I noted that the viewDidUnload
method of my MainView
was called while it was not happening when showing the other. It was the same case for the viewDidLoad
method (of MainView
) while closing the subviews.
我检查了打开和关闭子视图的两种方法,发现的唯一区别是在 modalTransitionStyle
中.在确实激活 viewDidUnload
/ viewDidLoad
的那个中,我使用的是 UIModalTransitionStyleFlipHorizontal
,而另一个在 UIModalTransitionStyleCrossDissolve
中.然后,我开始通过交换两种样式以及使用其他过渡样式来进行实验,发现这确实是导致不同行为的原因.实际上,当使用 UIModalTransitionStyleFlipHorizontal
时,总是调用父"视图控制器的 viewDidUnload
/ viewDidLoad
方法,而其他任何方法都从未发生过过渡样式.
I inspected the two methods for opening and closing the subviews and the only difference I found was in the modalTransitionStyle
. In the one which did activate viewDidUnload
/viewDidLoad
I was using UIModalTransitionStyleFlipHorizontal
and in the other UIModalTransitionStyleCrossDissolve
. I then started experimenting by swapping the two styles and by using the other transition styles as well and I found that this was indeed what was causing the different behaviour. In fact when using UIModalTransitionStyleFlipHorizontal
the viewDidUnload
/viewDidLoad
methods of the "parent" view controller were always called while it never happened for any of the other transition styles.
我的问题是,现在应该这样做,如果不是,那么正确的行为.是否应该在显示和关闭模式视图时调用unload和load方法.
My question is now it this is supposed to behave like this, and if not which behaviour is then the correct one. Are the unload and load methods supposed to get called when showing and closing an modal view or not.
还有,其他人可以确认或否定此行为吗?
Also, could someone else confirm or disconfirm this behaviour?
感谢
PS.每次调用两个被称为视图控制器的 viewDidLoad
方法.只是为了澄清:)
PS. the viewDidLoad
method of the two called view controllers gets called every time. Just to clarify :)
推荐答案
该行为应取决于当前的内存状况.如果内存不足,系统将向所有视图控制器发送内存警告.一个当前不在屏幕上的视图控制器将通过卸载其视图来对内存警告做出反应(显然,当再次出现在屏幕上时,将导致该视图的重新加载).
The behavior should depend on the current memory situation. If memory is low, the system will send memory warnings to all view controllers. A view controller whose view is not currently on screen will react on the memory warning by unloading its view (which will cause a reloading of the view when it comes on screen again, obviously).
因此,根据设备的内存状况和所显示的模式视图控制器的内存要求,卸载/加载行为应有所不同.它不应该依赖IMO的 modalTransitionStyle
.
So depending on the memory situation of the device and the memory requirements of the modal view controller you are displaying, the unload/load behavior should vary. It should not depend on the modalTransitionStyle
IMO.
这篇关于iPhone-viewDidUnload/viewDidLoad调用不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!