2011-10-31 12 views
0

プロジェクトの完全なオリエンテーションをしたいと思っていました。オリエンテーション:Xcode 4.2のポートレートと風景

#import "OrientationTutorialViewController.h" 

@implementation OrientationTutorialViewController 

@synthesize portraitView, landscapeView; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

} 

- (void) orientationChanged:(id)object 
{ 
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation]; 

    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.view = self.portraitView; 
    } 
    else 
    { 
     self.view = self.landscapeView; 
    } 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 



- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

この警告を修正する方法があります:コードだ

Warning

: 私はXcodeの上だ4.2 私の実装では、私に1回の警告を与えますか?

+0

[Xcode:警告 "列挙型uideviceorientationからの暗黙の変換"]の重複可能性(http://stackoverflow.com/questions/7015709/xcode-getting-warning-implicit-coversion-from-enumeration-type-uideviceorienta) ) –

答えて

3

このコードをthis tutorialからコピーしたと思います。これは、インターネット上のランダムな人からコードをコピーして貼り付けるだけの危険性を示しています。

このコードにはいくつか問題があります。まず、通知が-orientationメソッドがUIDeviceOrientation列挙型を返すUIDeviceを返すここで説明する問題があります。何らかの理由で、このコードの作成者はUIDeviceOrientation値として扱う代わりに、その値をUIInterfaceOrientation列挙型に割り当てています。これは、適切な列挙型を使用し、それらの値と比較することで修正できます。

第2に、UIViewControllerデリゲートメソッド-didRotateFromInterfaceOrientation:を簡単に使用できるようになったときに、オリエンテーションの変更に関する通知を使用するのはなぜですか?それはUIInterfaceOrientation列挙型で渡されます。上記の通知方法とレスポンダ方法を-didRotateFromInterfaceOrientation:に置き換えることをお勧めします。これを行う方法については、アップルの多くのビューコントローラのオートローテーションの例と豊富なドキュメントを参照してください。

第3に、上記の-orientationChanged:のような通知にメソッドが応答する場合、ジェネリックIDだけでなくNSNotificationオブジェクトも必要です。

+0

リンゴのexemplesを見て、ほとんどのものが最新のものであると言うのはちょっと簡単です。リンゴのドキュメントに "view controller autorotation"の検索に行きます。 何も見つかりませんでした あなたの非常に有用なヘルプ – a3116b

+0

@ a3116b - これに関するAppleのドキュメントは、iOS用のView Controller Programming Guideの「View Controllerのインタフェースオリエンテーションの管理」セクションなど、比較的簡単に見つかります。http://developer.apple.com/library/ios/featuredarticles /ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#// apple_ref/doc/uid/TP40007457-CH101-SW3。さらに、標準テンプレートのいずれかから新規アプリケーションを起動すると、自動的にこれらのメソッドがスタブアウトされます。 –

0

check out this postをよく説明しています。

また、必要に応じてオリエンテーションをキャストできます。

deviceOrientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation; 
+0

単純に型をキャストするのがこの問題の最良の解決策ではないと思います。これらの異なる列挙型の正確な数値が直接マップされていると仮定していますが、リンクされた答えにPengOneが示すように、真ではありません(UIDeviceOrientationには6つの値があり、UIInterfaceOrientationには4つしかありません)。より良いアプローチは、適切なデリゲートメソッドとUIInterfaceOrientation列挙型を使用することです。 –

0

私は、私はあなたにも、あなたがどこかで

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

を追加することに加えて、欲しいものにvariabel Initial interface orientationを変更してくださいする必要があることが分かった、これらの選択肢の多くを試してみました実装ファイル。スニペットは最初から働いていましたが、多くのビューとコントローラを追加すると、.plistを変更するまではすべてが台無しになりました。

関連する問題