2012-07-30 18 views
1

我在旋轉設備時嘗試更改視圖佈局時遇到了一些麻煩,我的主視圖叫做OverviewViewController,它包含2個視圖,當設備處於特定方向時它將隱藏/顯示。在子視圖中的shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{  
    // do some layout stuff 
    NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation"); 
    return YES; 
} 

每當我轉動我的設備執行該方法兩次,但在任何子視圖它從來不叫,例如;

// In OverviewViewController 

_landscapeView = [[LandscapeOverviewViewController alloc] initWithNibName:nil bundle:nil]; 
[self.view addSubview:_landscapeView.view]; 


// In LandscapeOverviewViewController 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{  
    NSLog(@"LandscapeOverviewViewController: shouldAutorateToInterfaceOrientation"); 

    // Only change orientation when it's landscape 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) return YES; 
    return NO; 
} 

是否有一個子視圖不響應設備方向的原因?該方法從來沒有在那裏調用。

+0

模態呈現這些視圖控制器,與此代碼,你說,在旋轉時,overviewViewController調用shouldAutorotateToInterfaceOrientation方法,但LandscapeOverviewViewController犯規通話shouldAutorotateToInterfaceOrientation? – 2012-07-30 03:29:18

+0

http://developer.apple.com/library/ios/#qa/qa1688/_index.html – Luke 2012-07-30 03:33:23

+0

@calvinBhai這正是我的意思,是的。 – 2012-07-30 03:36:22

回答

3

正如我所看到的,您將LandscapeOverviewViewController視圖作爲子視圖添加到self.view
而且你希望你的LandscapeOverviewViewControllershouldAutorotateToInterfaceOrientation被調用,永遠不會發生。
由於這些被稱爲該控制器是pushedpresented不喜歡你在做什麼。

解決方案: 你可以做兩件事情
1.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{  
    // do some layout stuff 
    NSLog(@"OverviewViewController shouldAutorotateToInterfaceOrientation"); 
    //Call explicitly shouldAutorotateToInterfaceOrientation of LandscapeOverviewViewController 
    [_landscapeView shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 
    return YES; 
} 


2.您可以在LandscapeOverviewViewController

+0

手動調用方法可以正常工作,但是這是一個很好的方法嗎? – 2012-07-30 03:39:10

+0

一切都很好,如果它的工作。但更好的方法是註冊方向更改通知。而且我個人在我的一個項目中也做了同樣的事情。 – 2012-07-30 03:42:01

0

註冊方位通知是需要你用兩個獨立的viewcontrollers (每個方向一個)在您的overviewViewController?

如果您只是根據方向更改幀值,請確保使用自動重設掩碼。

如果不是,則對每個方向使用不同的視圖,並根據需要隱藏取消隱藏每個視圖。

目前的問題是,你的lanscapeViewController沒有人發送它的viewControllerEvents。

與當前的設置,您可以在OverviewViewController

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration{ 
{  
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     [self presentModalViewController:LandscapeOverviewViewController animated: NO]; 
     } 
    else{ 
     [self presentModalViewController:PortraitOverviewViewController animated: NO]; 
     } 
} 
+0

視圖是佈局的一部分,並且在旋轉設備時完全不同,因此我無法將它們呈現爲模態視圖 – 2012-07-30 03:39:47

+0

此文檔不鼓勵使用此方法進行此類處理。 shouldAutorotateToInterfactOrientation應僅返回「是」/「否」。響應和更改應該在didRotate ..或其他相關協議中完成。 – 2012-07-30 03:41:58

+2

@DeanDavids你是對的。而不是在shouldAutoRotate中執行此操作,可以使用didRotate或willRotate方法執行相同操作。 – 2012-07-30 03:53:20

相關問題