2011-07-02 27 views
2

當我旋轉橫向時,顯示模式視圖控制器。我想在肖像中刪除模式視圖控制器。出於某種原因,當我進入肖像模式時,我的日誌語句不會出現。無法檢測到iPhone上的縱向方向

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
        interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || 
        interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
        interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 


-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
     toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { 

     NSLog(@"showing chart"); 
     [self presentModalViewController:landscapeChartViewController animated:NO]; 
    } 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
     toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
     NSLog(@"dismissing chart"); 
     [self.parentViewController dismissModalViewControllerAnimated:NO]; 
    } 
} 

回答

1

您可以簡化這些代碼,可能有助於縮小範圍。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations. 
return YES; // Return YES is the same as entering all interfaces. 
} 


-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 

    NSLog(@"showing chart"); 
    [self presentModalViewController:landscapeChartViewController animated:NO]; 
} 

if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
    NSLog(@"dismissing chart"); 
    [self.parentViewController dismissModalViewControllerAnimated:NO]; 
    // self.parentViewController seems like a call FROM the modalViewController. 
    // This should be moved to the modalViewControllers implementation 
} 
} 

只是看着它,我想你需要關閉模態視圖內的模態視圖控制器,而不是在父視圖內。因此,您可以使用主控制器內的橫向版本,然後將「willAnimateRotation ...」添加到模式控制器以處理縱向旋轉狀態。

+0

哇你的代碼更乾淨,邏輯正確。謝謝一堆。 –