我想让我的项目支持全定向。我使用的是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有没有办法修复这个警告?
发布于 2011-11-02 03:27:38
我猜你从this tutorial上复制了这段代码。这表明了仅仅从互联网上某个随机的人那里复制和粘贴代码的危险。
这段代码有一些问题。首先,这里有一个您在这里描述的问题,UIDeviceOrientationDidChangeNotification通知传递回一个UIDevice,它的-orientation方法返回一个UIDeviceOrientation枚举。由于某些原因,这段代码的作者将该值赋给UIInterfaceOrientation枚举,而不是将其作为UIDeviceOrientation值处理。这可以通过使用适当的枚举类型并与这些值进行比较来解决。
其次,当他们可以很容易地使用UIViewController委托方法-didRotateFromInterfaceOrientation:时,为什么他们要使用方向更改通知?它确实传入了一个UIInterfaceOrientation枚举。我建议用-didRotateFromInterfaceOrientation:替换上面的通知和响应器方法。查看Apple的许多视图控制器自动旋转的示例,以及他们丰富的文档,了解如何做到这一点。
第三,如果他们要让一个方法响应通知,就像上面的-orientationChanged:一样,它应该接受一个NSNotification对象,而不仅仅是一个泛型id。
发布于 2012-06-18 14:48:21
我尝试了很多这样的替代方法,然后我发现除了添加变量之外,还必须确保将变量Initial interface orientation更改为您想要的值
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}在您的实现文件中的某个位置。一开始只有代码片段可以工作,但是当添加更多的视图和控制器时,一切都变得一团糟,直到我更改了.plist。
https://stackoverflow.com/questions/7952402
复制相似问题