定位:肖像& Xcode 4.2中的景观

时间:2011-10-31 10:25:57

标签: ios cocoa-touch uiinterfaceorientation

我想让我的项目支持完全定位。 我在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

有没有办法解决此警告?

3 个答案:

答案 0 :(得分:3)

我猜你从this tutorial复制了这段代码。这表明只是从互联网上的一些随机人员复制和粘贴代码的危险。

此代码存在一些问题。首先,您在此处描述的问题是UIDeviceOrientationDidChangeNotification通知传回UIDevice,其-orientation方法返回UIDeviceOrientation枚举。出于某种原因,此代码的作者将该值分配给UIInterfaceOrientation枚举,而不是将其作为UIDeviceOrientation值处理。这可以通过使用适当的枚举类型并与这些值进行比较来解决。

其次,当他们使用UIViewController委托方法-didRotateFromInterfaceOrientation:时,为什么他们会使用通知进行方向更改?这确实传递了UIInterfaceOrientation枚举。我建议使用-didRotateFromInterfaceOrientation:替换上面的通知和响应方法。有关如何执行此操作,请参阅Apple的许多视图控制器自动旋转示例以及丰富的文档。

第三,如果他们要让方法响应通知,就像上面的-orientationChanged:一样,它应该采用NSNotification对象,而不仅仅是通用ID。

答案 1 :(得分:0)

check out this post,它解释得很好。

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

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

答案 2 :(得分:0)

我尝试了很多这些替代方案,然后我发现除了添加

之外,您还必须确保将变量Initial interface orientation更改为您想要的内容。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

您的实施文件中的某个地方。只是片段在开始时工作,但是当添加更多视图和控制器时,这一切都搞砸了,直到我更改了.plist。

相关问题