2011-11-16 12 views
0

我需要在appDidBecomeActive接口方向:(UIApplication的*)應用如何在appDidBecomeActive中獲取正確的界面方向:(UIApplication *)app?

[application statusBarOrientation]; 

,但如果應用程序從封閉開始(即不是從後臺重新開始),這總是返回畫像,它的工作原理與背景時恢復。

此外,我嘗試使用UIDevice方向以及狀態欄方向,但UIDevice方向可能不是接口方向。

那麼有沒有什麼辦法可以在app delegate appDidBecomeActive中獲得界面方向?

謝謝!

+0

什麼是你想在應用程序的委託呢?我問的原因是因爲界面方向幾乎總是用於視圖,這就是爲什麼它是UIViewController上的一個屬性...也許它可能會等到你呈現第一個視圖控制器? – Vinnie

+0

我需要顯示一個與默認飛濺圖像相同的保持視圖,但方向性很好,這就是爲什麼我需要知道應用程序代理中的方向。 – hzxu

回答

1

你需要做的是在你的飛濺視圖控制器中處理。您可以使用組合interfaceOrientation,shouldAutorotateToInterfaceOrientation,didAutorotateToInterfaceOrientation等。

本質上,創建一個視圖控制器,您將擁有作爲您的根視圖控制器。在那裏,確定shouldAutorotateToInterfaceOrientation中的方向更改(它將始終是viewDidLoad中的縱向或橫向,具體取決於您的xib,因此請勿在此處執行此操作)。用NSTimer或其他方法顯示你的圖像。計時器後,顯示您的常規應用程序屏幕。

無論如何,只有擁有視圖控制器才能顯示圖像,因此您必須等到視圖控制器爲您提供interfaceOrientation更改。您應該關注第一個視圖控制器,而不是應用程序代理。

AppDelegate.h

#import <UIKit/UIKit.h> 

@class SplashViewController; 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (retain, nonatomic) IBOutlet UIWindow *window; 
@property (retain, nonatomic) SplashViewController *splashController; 

-(void)showSplash; 
@end 

AppDelegate.m

#import "AppDelegate.h" 
#import "SplashViewController.h" 

@implementation AppDelegate 
@synthesize window = _window, splashController = _splashController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    [self showSplash]; 
    [self.window makeKeyAndVisible]; 
    [self performSelector:@selector(registerBackground) withObject:nil afterDelay:5.0]; 
    return YES; 
} 

-(void)showSplash 
{ 
    SplashViewController *splash = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil]; 
    [self.window addSubview:splash.view]; 
    self.splashController = splash; 
    [splash release]; 
    //have to add a delay, otherwise it will be called on initial launch. 
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(removeSplash:) userInfo:nil repeats:NO]; 

} 

-(void)registerBackground 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(returnFromBackground:) 
               name:UIApplicationDidBecomeActiveNotification 
               object:nil]; 
} 

-(void)returnFromBackground:(NSNotification *)notification 
{ 
    [self showSplash]; 
} 

-(void)removeSplash:(NSTimer *)timer 
{ 
    [self.splashController.view removeFromSuperview]; 
    self.splashController = nil; 
} 


- (void)dealloc 
{ 
    [_window release]; 
    [_splashController release]; 
    [super dealloc]; 
} 
+0

當然,但我正在使用持有視圖並將其添加到窗口而不是控制器,因此我必須在應用程序委託中檢測方向 – hzxu

+0

爲什麼不讓視圖控制器的持有視圖成爲一部分?如果你這樣做,你應該接收界面方向調用:[self.window addSubView:myHoldingController.view];您也可能會使用NSNotification調用。 – Vinnie

+0

我接受你的答案,因爲它迄今是最好的答案,但我不能使用視圖控制器,因爲即使應用程序從後臺恢復,我也需要顯示持有視圖,所以主視圖控制器可能不是可見/頂視圖控制器。 – hzxu