簡體   English   中英

方向:Xcode 4.2中的縱向和橫向

[英]Orientation : portrait & landscape in Xcode 4.2

我想讓我的項目支持完全定向。 我正在使用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

有沒有辦法解決此警告?

我猜您是從本教程復制了此代碼。 這顯示了僅從Internet上某個隨機人復制並粘貼代碼的危險。

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

其次,為什么他們使用通知來更改方向,而他們-didRotateFromInterfaceOrientation:容易使用UIViewController委托方法-didRotateFromInterfaceOrientation:呢? 這確實傳遞了UIInterfaceOrientation枚舉。 我建議使用-didRotateFromInterfaceOrientation:替換上面的通知和響應者方法。 有關如何執行此操作的信息,請參閱Apple的許多視圖控制器自動旋轉示例,以及它們的大量文檔。

第三,如果他們要有一種方法來響應通知,例如上面的-orientationChanged:它應該使用NSNotification對象,而不僅僅是通用ID。

我嘗試了很多這樣的選擇,然后發現除了添加之外,您還必須確保將variabel Initial interface orientation更改為所需的Initial interface orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

實現文件中的某個位置。 只是片段在開始就起作用,但是當添加更多視圖和控制器時,直到我更改.plist為止,一切都弄糟了。

看看這篇文章 ,它解釋得很好。

另外,如果需要,您可以投射方向。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM