2012-03-19 12 views
8

お願いします。ViewDidLoadのiphoneデバイスの現在の向き

-(void) getCurrentOrientation{ 

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    if(orientation == UIInterfaceOrientationPortrait){ 
     NSLog(@"portrait"); 
    } else if(orientation == UIInterfaceOrientationLandscapeRight) { 
     NSLog(@"LandscapeRight"); 
    } else if(orientation == UIInterfaceOrientationLandscapeLeft) { 
     NSLog(@"LandscapeLeft"); 
    } 
} 

が、私はこのgetCurrentOrientationは、のviewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self getCurrentOrientation]; 

} 

のNSLogが空であるを投げる呼び出す:私は、メソッドを持っています。どうしましたか ? 私もお試しください

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)) { 
     NSLog(@"portrait"); 

    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) 

    { 
     NSLog(@"LandscapeRight"); 
    } 

} 

しかし、そのvarintも空です。

私はどのオリエントユーザーがAPPを起動するのか知っておく必要があります。

私に助言をしてください。

答えて

15

あなたのコードは間違いなく、問題はありません。

この文は元のデバイスでのみ機能します。私はデバイスとシミュレータの両方であなたのためにテストして

:あなたはシミュレータで確認したいしかし

あなたは

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { // portrait } else { // landscape } 

などの更新を確認することができます。最初は、デバイスをランドスケープに保ちますが、viewDidLoadでのみポートレートが表示されます。 First ControllerはshouldAutoRotateメソッドを調べます。

viewDidLoadの初期オリエンテーションを使用しないでください。正確な方向付けを行うには、まずshouldAutorotateメソッドに依存する必要があります。

+0

ありがとうございます。しかし、私がLANDSCAPEモードでシミュレータを実行すると、NSlogはPORTRAITを出力します。したがって、実際のデバイスでの作業を修正するかどうかはわかりません –

+0

更新された回答を確認してください –

+0

プロジェクト - ターゲット - 概要 - サポートされているデバイスオリエンテーションのみランドスケープで選択します。しかし、あなたの関数Nslogの出力ポートレートでsumulatorでappを実行すると、なぜ私は頼りにしてはいけないのか?私はちょうどオリエンテーションのユーザーがアプリを実行する知っている必要があります。 –

2

これはあなたを助けるかもしれshouldAutorotate

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation 
    { 
     if(interfaceOrientation == UIInterfaceOrientationPortrait){ 
      NSLog(@"portrait");  
     } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) {  
      NSLog(@"LandscapeRight");   
     } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {  
      NSLog(@"LandscapeLeft"); 
     }   
     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) 
    } 

以下でごshouldAutorotateMethodを交換してください。

+0

ちょうど私に教えてください、なぜですか? shouldAutorotateToInterfaceOrientationに問題はありません。 viewDidLoadに問題があります。 –

1

あなただけの次のコードを使用し、その後アプリの向きを確認したい場合:

- (void) viewDidLoad { 
    [super viewDidLoad]; 
    BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation); 
    // now do whatever you need 
} 

または

-(void)viewDidLoad 
{ 
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) 
    { 
     //landscape view code 
    } 
    else 
    { 
     //portrait view code 
    } 
} 
1

をごviewDidLoadと呼ばれていますか?そこにブレークポイントを入れましたか?

印刷するとどうなりますかNSLog(@"%i", [[UIDevice currentDevice] orientation])

beginGeneratingDeviceOrientationNotificationsを呼び出さなかった場合、オリエンテーションは常に0を返します。あなたがオリエンテーションを取得しようとする直前にそれを呼び出すかもしれませんが、十分ではありません。電話をapplication:didFinishLaunchingWithOptions:に移動してください。

ただし、コントローラーが指定する方向を使用するのが最も良い方法です([UIViewController interfaceOrientation])。パラメーターをshouldAutorotateToInterfaceOrientationに渡します。

+0

ありがとうございます。はい方向は常に0を返し、私は(上記のように)beginGeneratingDeviceOrientationNotificationsを配置します。 [:didFinishLaunchingWithOptions:UIApplicationDelegateアプリケーション]しかし向きはまた、私はあなたが 'に移動しようとするべきであると言っている理由です0 –

+0

返す' – Sulthan

+0

おかげで、しかし、この方法はunusefullある –

1

アプリ最初のロードは、UIDevice.current.orientationが有効ではありません。しかし、UIApplication.shared.statusBarOrientationはです。 UIDevice.current.orientationは一般的に向きを確認するより良い方法です。だから、このメソッドはすべての状況を処理します

func isLandscape() -> Bool { 
    return UIDevice.current.orientation.isValidInterfaceOrientation 
     ? UIDevice.current.orientation.isLandscape 
     : UIApplication.shared.statusBarOrientation.isLandscape 
} 
関連する問題