我现在真是一团糟。我使用了执行此操作的苹果示例代码:
- 创建纵向视图控制器和横向视图控制器
- Potrait 事件控制器然后注册设备方向更改通知
- 当设备旋转时,它会为横向视图呈现一个模式视图控制器,或者如果它旋转回纵向则关闭横向视图。
除了一个小问题之外,一切都正常工作....
现在到我的问题。我用它从表格视图中启动了一个可旋转的视图控制器。它可以旋转并且工作正常。但如果我最初以横向模式启动它,它仍会以纵向模式启动。如果我想要风景,我必须在之后再次将其旋转到风景。我非常努力地解决这个问题但失败了。您可以从Apple Developer Site Here下载并运行示例代码。任何人都可以修复此代码,以便如果在横向模式下启动它会呈现横向视图的模式视图?否则我将不得不重写所有内容以使用单个视图控制器。这些是苹果代码的相关部分:
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithRed:197.0/255.0 green:204.0/255.0 blue:211.0/255.0 alpha:1.0];
LandscapeViewController *viewController = [[LandscapeViewController alloc]
initWithNibName:@"LandscapeView" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return (interfaceOrientation == UIInterfaceOrientationPortrait); // support only portrait}