为什么iOS自动旋转由didReceiveMemoryWarning发布后从Nib加载的视图? [英] Why Doesn't iOS Autorotate a View loaded from a Nib after it was released by didReceiveMemoryWarning?
问题描述
我的iPad应用程序大量使用自动旋转。这很棒。但是,我注意到如果隐藏视图是由 didReceiveMemoryWarning
的默认实现释放的(如所述这里,当从笔尖重新加载视图时我碰巧在风景中,它以纵向加载它。这会对界面造成严重破坏,直到我手动旋转iPad并强制它进入正确的方向。
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.
我以为iOS会在当前方向加载视图;这就是应用程序启动时的功能。但它没有,不是在被 didReceiveMemoryWarning
卸载之后。为什么不?我怎么能让它做到这一点?
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?
推荐答案
答案,感谢dbarker ,是视图控制器的旋转方法,包括 -willRotateToInterfaceOrientation:duration:
和 -willAnimateRotationToInterfaceOrientation:duration:
,在默认实现 didReceiveMemoryWarning
卸载视图后重新加载视图时不会被调用。我不知道为什么它在应用程序启动时会有所不同,但我确实有一个解决方法
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 of didReceiveMemoryWarning
has unloaded the view. I've no idea why it would be different on app launch, but I do have a workaround
我所做的是设置一个名为<$ c $的布尔值。 c> unloadedByMemoryWarning ,在 didReceiveMemoryWarning
中 YES
,如下所示:
What I did was to set a boolean ivar, named unloadedByMemoryWarning
, to YES
in didReceiveMemoryWarning
, like so:
- (void) didReceiveMemoryWarning {
unloadedByMemoryWarning = YES;
[super didReceiveMemoryWarning];
}
然后,在 viewDidLoad
,如果该标志为真,我将其设置为否
,然后自己调用旋转方法:
Then, in viewDidLoad
, if that flag is true, I set it to NO
and then call the rotation methods myself:
if (unloadedByMemoryWarning) {
unloadedByMemoryWarning = NO;
[self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
[self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0];
[self didRotateFromInterfaceOrientation:self.interfaceOrientation];
}
有点糟透了我必须这样做,但确实有效,现在我不太担心因使用太多内存而被iOS杀死。
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自动旋转由didReceiveMemoryWarning发布后从Nib加载的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!