为什么iOS不会在由didReceiveMemoryWarning发布之后自动从Nib加载的视图?

the*_*ory 5 uiviewcontroller didreceivememorywarning autorotate ios

我的iPad应用程序大量使用自动旋转.这很棒.然而,我注意到,如果一个隐藏的视图是由默认实现释放didReceiveMemoryWarning(如描述在这里),当画面从笔尖重新加载,我正好是在景观,它加载它的肖像.这会对界面造成严重破坏,直到我手动旋转iPad并强制它进入正确的方向.

我原以为iOS会在当前方向加载视图; 这就是应用程序启动时的功能.但它没有,不是在被卸下之后didReceiveMemoryWarning.为什么不?我怎么能这样做呢?

the*_*ory 4

通过dbarker的指针确定的答案是,在 的默认实现卸载视图后重新加载视图时,视图控制器的旋转方法(包括-willRotateToInterfaceOrientation:duration:和)不会被调用。我不知道为什么应用程序启动时会有所不同,但我确实有一个解决方法-willAnimateRotationToInterfaceOrientation:duration:didReceiveMemoryWarning

我所做的是将一个名为 , 的布尔 ivar 设置unloadedByMemoryWarningYESin didReceiveMemoryWarning,如下所示:

- (void) didReceiveMemoryWarning {
    unloadedByMemoryWarning = YES;
    [super didReceiveMemoryWarning];
}
Run Code Online (Sandbox Code Playgroud)

然后,在 中viewDidLoad,如果该标志为 true,我将其设置为NO,然后自己调用旋转方法:

if (unloadedByMemoryWarning) {
    unloadedByMemoryWarning = NO;
    [self willRotateToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0];
    [self didRotateFromInterfaceOrientation:self.interfaceOrientation];
}
Run Code Online (Sandbox Code Playgroud)

我必须这样做有点糟糕,但它确实有效,现在我不太担心因为使用太多内存而被 iOS 杀死。