首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >方向: Xcode 4.2中的纵向和横向

方向: Xcode 4.2中的纵向和横向
EN

Stack Overflow用户
提问于 2011-10-31 18:25:57
回答 2查看 4K关注 0票数 0

我想让我的项目支持全定向。我使用的是xcode 4.2,我的实现给了我一个警告:

这就是代码:

代码语言:javascript
运行
复制
#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

有没有办法修复这个警告?

EN

回答 2

Stack Overflow用户

发布于 2011-11-02 03:27:38

我猜你从this tutorial上复制了这段代码。这表明了仅仅从互联网上某个随机的人那里复制和粘贴代码的危险。

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

其次,当他们可以很容易地使用UIViewController委托方法-didRotateFromInterfaceOrientation:时,为什么他们要使用方向更改通知?它确实传入了一个UIInterfaceOrientation枚举。我建议用-didRotateFromInterfaceOrientation:替换上面的通知和响应器方法。查看Apple的许多视图控制器自动旋转的示例,以及他们丰富的文档,了解如何做到这一点。

第三,如果他们要让一个方法响应通知,就像上面的-orientationChanged:一样,它应该接受一个NSNotification对象,而不仅仅是一个泛型id。

票数 3
EN

Stack Overflow用户

发布于 2012-06-18 14:48:21

我尝试了很多这样的替代方法,然后我发现除了添加变量之外,还必须确保将变量Initial interface orientation更改为您想要的值

代码语言:javascript
运行
复制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

在您的实现文件中的某个位置。一开始只有代码片段可以工作,但是当添加更多的视图和控制器时,一切都变得一团糟,直到我更改了.plist。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7952402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档