为什么 iOS 不自动旋转由 didReceiveMemoryWarning 释放后从 Nib 加载的视图?
我的 iPad 应用大量使用自动旋转。这太棒了。但是,我注意到,如果 didReceiveMemoryWarning
的默认实现释放了隐藏视图(如所述此处),当从笔尖重新加载视图并且我碰巧处于横向时,它会加载它的肖像。这会对界面造成严重破坏,直到我手动旋转 iPad 并强制它转到正确的方向。
我曾假设 iOS 会以当前方向加载视图;但事实并非如此。这就是应用程序启动时所做的事情。但它不,不是在被 didReceiveMemoryWarning
卸载后。为什么不呢?我怎样才能让它做到这一点?
My iPad app makes heavy use of autorotation. This is great. However, I've noticed that if a hidden view is released by the default implementation of didReceiveMemoryWarning
(as described here), when the view is re-loaded from the nib and I happen to be in landscape, it loads it in portrait. This wreaks havoc with the interface until I rotate the iPad manually and force it to go to the proper orientation.
I had assumed that iOS would load the view in the current orientation; that's what it does when the app launches. But it no, not after being unloaded by didReceiveMemoryWarning
. Why not? And how can I get it to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过 dbarker 的指针确定的答案是视图控制器的旋转方法,包括
-willRotateToInterfaceOrientation:duration :
和-willAnimateRotationToInterfaceOrientation:duration:
,在didReceiveMemoryWarning
的默认实现卸载视图后重新加载视图时,不会被调用。我不知道为什么它在应用程序启动时会有所不同,但我确实有一个解决方法我所做的是将一个名为
unloadedByMemoryWarning
的布尔 ivar 设置为YES
didReceiveMemoryWarning
,如下所示:然后,在
viewDidLoad
中,如果该标志为 true,我将其设置为NO
,然后自己调用旋转方法:我必须这样做有点糟糕,但它确实有效,现在我不太担心因为使用太多内存而被 iOS 杀死。
The answer, determined thanks to pointers from dbarker, is that the view controller's rotation methods, including
-willRotateToInterfaceOrientation:duration:
and-willAnimateRotationToInterfaceOrientation:duration:
, will not be called when a view is reloaded after a the default implementation ofdidReceiveMemoryWarning
has unloaded the view. I've no idea why it would be different on app launch, but I do have a workaroundWhat I did was to set a boolean ivar, named
unloadedByMemoryWarning
, toYES
indidReceiveMemoryWarning
, like so:Then, in
viewDidLoad
, if that flag is true, I set it toNO
and then call the rotation methods myself:Kinda sucks that I have to do this, but it does work, and now I'm less concerned about getting killed by iOS for using too much memory.
我认为 iOS 5 可能会解决这个问题。
对于 iOS 4.3,我很幸运地进行了另一次修复。从笔尖加载后:
如果有效,您可以放弃
unloadedByMemoryWarning
逻辑,因为每次加载都是安全的。得到了提示&代码(基本上)来自此处。I think iOS 5 might fix this.
For iOS 4.3, I've had good luck with another fix. After the load from nib:
If that worked, you could jettison the
unloadedByMemoryWarning
logic, since it's safe to do every load. Got the tip & code (basically) from here.