1

我不太明白为什么会遇到这个问题,但我是……基本上我有一个 Tab 栏控制器,navigationController其中一个选项卡中有一个。在这个特定的选项卡中,我希望用户能够旋转设备并看到完全不同的视图,而不是旋转的原始视图!

为了实现这一点(全屏)LandscapeViewController,当设备处于横向时,我会以模态方式呈现它。当我提出modalViewController…</p>

在中,LandscapeViewController我还检查了方向,以便在用户返回纵向模式时关闭自我。但是,当我展示它时,我收到一个回调,说它正在进入纵向模式,这完全搞砸了!

这是代码和日志语句,以澄清发生了什么..

肖像视图控制器

(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
  NSLog(@"Norm going Portrait");

  [self changeTheViewToPortrait:YES andDuration:duration];
  
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  NSLog(@"Norm going Landscape");
  [self changeTheViewToPortrait:NO andDuration:duration];
    }
}


(void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration {
 
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.];
 
    if(portrait){ 
  NSLog(@"dismissed");
  [self dismissModalViewControllerAnimated:NO];
    }
    else{ 
  NSLog(@"presented");
  LandscapeOverviewViewController *land = [[LandscapeOverviewViewController alloc]initWithNibName:@"LandscapeOverviewViewController" bundle:nil];
  land.delegate = self;
  [self presentModalViewController:land animated:NO]; 
  [land release];
    }
 
    [UIView commitAnimations];
}

景观视图控制器

 (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
  NSLog(@"Modal is going Portrait");
  [self changeTheViewToPortrait:YES andDuration:duration];
  
    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  NSLog(@"Modal is going Landscape");

  [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

 (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
 
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
 
    if(portrait){ 
  NSLog(@"calling delegate to dismiss");
  [delegate landscapeViewController:self didRotateBack:(YES)]; 
    }
 
    [UIView commitAnimations];
}

这是日志文本:

常态化景观

2010-08-24 15:29:40.024 应用程序 [32461:207] 提交

2010-08-24 15:29:40.026 应用程序 [32461:207] 模态正在进行中

2010-08-24 15:29:40.026 App[32461:207] 调用代表解散

2010-08-24 15:29:40.027 App[32461:207] 委托调用

正如您在纵向时看到的那样,我通过呈现 来旋转它做正确的事情landscapevc,但随后它认为它是纵向的并试图解雇。

谁能看到我哪里出错了?(并且对此长度表示歉意,但代码的格式将无法正常工作。)

4

1 回答 1

1

您是否在横向视图控制器中实现了 shouldAutorotateToInterfaceOrientation: 方法?如果没有,这个视图控制器将被迫停留在纵向模式,这反过来又会触发你的解雇代码。

还值得注意的是,有时视图控制器会以特定方向添加到窗口中,然后进行旋转。根据当前旋转检查 willRotate 传递给您的方向是值得的。我已经有一段时间没有验证这一点了,但是在 2.x 后期/3.0 早期,这很常见。

于 2010-08-24T16:52:48.307 回答