尝试子类导航控制器,以便在iOS中的横向模式下加载外部视图控制器与单独的.xib文件

tvmytwxo  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(86)

我有一个iPad应用程序,我用故事板创建。我创建了另一个viewController,它是使用单独的.xib文件创建的。我需要从主应用程序调用这个viewController,然后关闭它以返回主应用程序。到目前为止,我能够做到这一点。
我的问题是,因为我正在使用一个导航控制器来调用这个辅助视图控制器,所以我无法在横向模式下加载这个视图控制器。我只能在肖像模式下加载它。通过浏览这个论坛,以及我所做的任何研究,我了解到我需要子类化导航控制器,然后这就是我如何能够在横向模式下加载这个辅助视图控制器。
我已经在我的辅助视图控制器(NextViewController)中包含了以下方法,但它没有效果:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

下面是调用viewController(MainViewController)的代码,它调用NextViewController,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,并有它显示在横向模式?

wko9yo5t

wko9yo5t1#

对于导航控制器的方向子类,您可以尝试以下代码(作为示例):

// .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上工作)

相关问题