0

我有使用故事板创建的iPad应用程序。我创建了另一个单独的viewController,它使用单独的.xib文件创建。这个viewController我需要从主应用程序调用,然后再解散返回到主应用程序。我能够做到这一点。我的问题是,因为我正在使用导航控制器来调用这个辅助视图控制器,我无法以横向模式加载此视图控制器。我只能以纵向模式加载它。基于经历这个论坛,以及我所做的任何研究,我已经了解到我需要继承导航控制器,然后我就可以在横向模式下加载这个辅助视图控制器。尝试子类化导航控制器以加载外部视图控制器在iOS中处于横向模式下具有单独的.xib文件

我已经包含在我的辅助视图控制器(NextViewController)下面的方法,但它没有任何效果:

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

这里是在主叫的viewController(MainViewController),它调用NextViewController,代码,其在又出现在肖像模式,而不是期望的风景模式:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    _nextView = [[NextLandscapeViewController alloc] initWithNibName:@"NextLandscapeViewController" bundle:nil]; 
    [_nextView setDelegate:(id)self]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_nextView]; 
    [self presentViewController:navigationController animated:YES completion:nil]; 

} 

正如我指出的,我需要的解决方案是继承导航控制器,但老实说,我从来没有这样做过,并且也没有我知道该怎样 去做吧。有人可以告诉我怎么做,这样我可以调用NextViewController,并以横向模式显示它?

在此先感谢所有回复的人。

回答

1

有关的导航控制器子类的方向,你可以试试这个代码(为例):

// .h - file 
@interface MyNavigationController : UINavigationController 

@end 

// .m - file 
#import "MyNavigationController.h" 

@implementation MyNavigationController 

-(BOOL)shouldAutorotate 
{ 
    return [self.topViewController shouldAutorotate]; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return [self.topViewController supportedInterfaceOrientations]; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
} 

@end 

UPD(在iOS6的这段代码工作)

+0

非常感谢您的及时答复。你的代码正在工作,但我注意到一个小故障。它的工作原理与我第一次运行时完全相同,但是,在后续运行中,现在整个应用程序都以横向模式显示。我只需要被调用的视图控制器仅处于横向模式。有没有办法纠正这个问题? – syedfa

+0

嗯..我不知道..我做了下一种方式 - 我在方法supportedInterfaceOrientations返回纵向方向时创建Coommon viewcontroller。所有VC分类从它期望一些需要水平方向。在我的情况下,它的工作。你可以尝试做。 – frankWhite

相关问题