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

相關問題