2012-11-16 50 views
1

我有點困惑。 我想我的應用程序正好在我的5個View Controller中的一箇中旋轉。 我創建了一個類別的UINavigationController實現在IOS 6shouldAutorotate沒有被調用,雖然我有類的uinavigationcontroller轉發呼籲

#import "UINavigationController+IOS6Rotation.h" 


@implementation UINavigationController (IOS6Rotation) 

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    if (INTERFACE_IS_PAD) { 
     return UIInterfaceOrientationLandscapeLeft; 
    } 
    else{ 
     return UIInterfaceOrientationPortrait; 
    }  
} 
@end 

我也有我的所有ViewControllers不應旋轉實現了這些方法旋轉必要的方法。

- (BOOL)shouldAutorotate { 
    return NO; 
} 

- (BOOL)supportedInterfaceOrientations { 
    if (INTERFACE_IS_PAD) { 
     return UIInterfaceOrientationMaskLandscape; 
    } 
    else{ 
     return UIInterfaceOrientationMaskPortrait; 
    } 
} 

誰應該轉動;是在shouldAutorotate

啓動時的一個,我使用一個額外的viewController顯示一個閃屏。這個SplashScreen像這樣顯示爲RootViewController。

vc_splash = [[SplashScreenViewController alloc] initWithNibName:@"SplashScreenViewController" bundle:[NSBundle mainBundle]]; 
[self.window setRootViewController:vc_splash]; 

DefaultSHKConfigurator *configurator = [[MySHKConfigurator alloc] init]; 
[SHKConfiguration sharedInstanceWithConfigurator:configurator]; 
[self.window makeKeyAndVisible]; 

當我完成加載我的數據庫,splashscreen交換爲主屏幕。

self.viewController = [[MainViewController alloc] initWithNibName:@"MainViewController_iPhone" bundle:nil]; 
    UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: self.viewController]; 
    [self.window setRootViewController: navControl]; 

現在的問題是,只有我的飛濺屏幕調用shouldAutorotate,但其他屏幕沒有。 有人可以告訴我,我是否錯過了這裏的一些必需品? 我想我已經做了所有必要的東西來獲取BL ****Ÿ自轉在iOS 6的正常工作......

Thx提前, Maverick1st

**更新* * 如果你希望你的iPad在景觀開始​​,一定要在你的shouldAutoRotate返回YES,

+0

你爲什麼要實施'UINavigatorController'類別,當你實現了所有的方法,在5'ViewController's的4呢?我認爲實現該類別的目的是爲了避免重複代碼。既然你走了這麼遠,爲什麼不直接在你想旋轉的'ViewController'中直接實現三個旋轉方法呢? – Bill

+0

您需要轉發類別中的方法調用,因爲當viewcontroller包含在tabbarcontroller或navigationcontroller中時,viewcontroller中的方法不會被調用。我已經在選項卡中使用tabbar和navigationcontroller創建了一個應用程序,並且它工作正常。它根本不知道我這次做錯了什麼,因爲它應該在我看來是正確的。 – Maverick1st

回答

3

,這是爲我工作在application添加supportedInterfaceOrientationsForWindow 方法:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController; 
    [navigationController shouldAutorotate]; 
    [navigationController preferredInterfaceOrientationForPresentation]; 
    return UIInterfaceOrientationMaskAll; 
} 

而且在applicationdelegate補充一點:

@implementation UINavigationController (Rotation_IOS6) 

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
} 
@end 
+0

從supportedIntefaceOrientationsForWindow顯式調用shouldAutorotate會在我的代碼中產生一個infinit循環。 我也有一個類別,所以我沒有理由在appDelegate中執行第二個操作。或者[self.viewcontrollers lastObject]和self.topViewController之間有區別嗎? – Maverick1st

+0

[self.viewcontrollers lastObject]'和'self.topViewController'之間沒有區別。從其他視圖控制器中刪除類別並僅在appDelegate –

+1

中添加,但這並不能解決問題,因爲supportedInterfaceOrientationsForWindow一直在調用beeing,所以您的代碼會導致無限循環。 – Maverick1st

相關問題