2011-10-31 82 views
0

我想讓我的項目支持完整的方向。 我上的Xcode 4.2 我的執行給了我一個警告:定位:縱向和橫向在Xcode 4.2

Warning

是這樣的代碼:

#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 

有沒有辦法解決這個警告?

+0

可能重複[Xcode:從「枚舉類型uideviceorientation」獲取警告「隱式覆蓋」](http://stackoverflow.com/questions/7015709/xcode-getting-warning-implicit-coversion-from-enumeration-type-uideviceorienta ) –

回答

3

我猜你從this tutorial複製了這段代碼。這表明只是從互聯網上的一些隨機人員複製和粘貼代碼的危險。

這段代碼有幾個問題。首先,您在此處描述的問題是UIDeviceOrientationDidChangeNotification通知返回的UIDevice,其-orientation方法返回UIDeviceOrientation枚舉。出於某種原因,此代碼的作者將該值分配給UIInterfaceOrientation枚舉,而不是將其作爲UIDeviceOrientation值處理。這可以通過使用適當的枚舉類型並與這些值進行比較來解決。

其次,爲什麼他們使用通知進行方向更改,當他們同樣容易地使用UIViewController委託方法-didRotateFromInterfaceOrientation:?這確實通過了UIInterfaceOrientation枚舉。我建議用-didRotateFromInterfaceOrientation:替換上面的通知和響應方法。有關如何執行此操作的信息,請參閱Apple的許多視圖控制器自動轉換示例以及其豐富的文檔。第三,如果他們要有一個方法響應通知,就像上面的-orientationChanged:一樣,它應該接受一個NSNotification對象,而不僅僅是一個通用的id。

+0

有點容易說,去看看蘋果的例子,其中大多數是最新的,我只是去蘋果文檔搜索關於「視圖控制器自轉」 沒有發現 謝謝你your're非常有用的幫助 – a3116b

+0

@ a3116b - 蘋果有關這方面的文檔相對比較容易找到,例如適用於iOS的視圖控制器編程指南中的「管理視圖控制器的界面方向」部分: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

我不認爲簡單的鑄造類型是解決這個問題的最佳方法。你假設這些不同的枚舉的確切數值直接映射,這是不正確的(UIDeviceOrientation有六個值,其中UIInterfaceOrientation只有四個值),正如PengOne在鏈接的答案中所示。更好的方法是使用適當的委託方法和UIInterfaceOrientation枚舉。 –

0

我已經嘗試了這麼多,這些替代品的,後來我發現,你還必須確保到variabel Initial interface orientation改爲你想在除了如何在增加

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

某個地方實施文件。只是一小段工作在開始,但是當添加更多的視圖和控制器時,這一切都搞砸了,直到我改變.plist。