2011-01-11 57 views
5

My iPad app大量使用自动旋转。这很棒。但是,我注意到,如果隐藏视图是由didReceiveMemoryWarning(如here所述)的默认实现释放的,那么当视图从笔尖重新加载并且恰好处于横向时,它将以纵向加载它。这会对接口造成巨大的破坏,直到我手动旋转iPad并强制其进入正确的方向。为什么iOS自动从视图加载的视图被didReceiveMemoryWarning释放后?

我假设iOS会以当前方向加载视图;这就是应用程序启动时的功能。但它没有,没有被didReceiveMemoryWarning卸载后。为什么不?我怎么才能做到这一点?

+0

视图层次结构之外的视图(而不是UIViewController视图的子视图)? – dstnbrkr 2011-01-11 18:28:27

+0

@dbarker - 不,实际上它是应用程序的主要视图。 – theory 2011-01-11 20:07:34

回答

4

答案,从dbarker确定感谢指针,是当一个视图的didReceiveMemoryWarning一个默认的实现已卸载的观点后重新加载视图控制器的旋转方法,包括-willRotateToInterfaceOrientation:duration:-willAnimateRotationToInterfaceOrientation:duration:,不会被调用。我不知道为什么它会是在应用程序启动不同的,但我有一个解决办法

我所做的就是设置一个布尔伊娃,命名unloadedByMemoryWarning,以YESdidReceiveMemoryWarning,像这样:

- (void) didReceiveMemoryWarning { 
    unloadedByMemoryWarning = YES; 
    [super didReceiveMemoryWarning]; 
} 

然后,在viewDidLoad,如果该标志为真,我把它设置为NO,然后调用旋转方法自己:

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

有点儿吮吸,我要做到这一点,但它的工作,现在我“M不太担心因使用太多内存而被iOS杀死。

1
  1. 我认为iOS 5可能会解决这个问题。

  2. 对于iOS 4.3,我已经有了另一个修复程序的好运。从笔尖后负荷:

    [parent.view addSubview:nibView]; 
    nibView.frame = parent.view.frame; 
    [nibView setNeedsLayout]; 
    

    是否奏效,你可以抛弃unloadedByMemoryWarning逻辑,因为它是安全的做好每一负载。代码(基本上)从here得到了提示&。

相关问题