2011-01-11 36 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得到了提示&。

相關問題