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

哇你的代码更干净,逻辑正确。谢谢一堆。 –