2011-07-02 7 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

あなたのコードははるかにクリーンであり、ロジックは正しいです。本当にありがとう。 –