简体   繁体   English

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

[英]Orientation : portrait & landscape in Xcode 4.2

I wanted to make my project support full orientation. 我想让我的项目支持完全定向。 I'm on xcode 4.2 My implementation gives me one warning: 我正在使用xcode 4.2,我的实现给了我一个警告:

警告

that's the code : 那是代码:

#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

Is there a way to fix this warning? 有没有办法解决此警告?

I'm guessing you copied this code from this tutorial . 我猜您是从本教程复制了此代码。 This shows the danger of just copying and pasting code from some random person on the Internet. 这显示了仅从Internet上某个随机人复制并粘贴代码的危险。

There are a few problems with this code. 此代码存在一些问题。 First, there's the issue you describe here, where the UIDeviceOrientationDidChangeNotification notification passes back a UIDevice, whose -orientation method returns a UIDeviceOrientation enum. 首先,这里有您描述的问题,其中UIDeviceOrientationDidChangeNotification通知传回一个UIDevice,其-orientation方法返回一个UIDeviceOrientation枚举。 For some reason, the author of this code is assigning that value to a UIInterfaceOrientation enum, instead of dealing with it as a UIDeviceOrientation value. 由于某种原因,此代码的作者正在将该值分配给UIInterfaceOrientation枚举,而不是将其作为UIDeviceOrientation值处理。 This could be fixed by using the appropriate enum type and comparing against those values. 这可以通过使用适当的枚举类型并与这些值进行比较来解决。

Second, why are they using a notification for orientation changes, when they just as easily could be using the UIViewController delegate method -didRotateFromInterfaceOrientation: ? 其次,为什么他们使用通知来更改方向,而他们-didRotateFromInterfaceOrientation:容易使用UIViewController委托方法-didRotateFromInterfaceOrientation:呢? That does pass in a UIInterfaceOrientation enum. 这确实传递了UIInterfaceOrientation枚举。 I recommend replacing the notification and the responder method above with -didRotateFromInterfaceOrientation: . 我建议使用-didRotateFromInterfaceOrientation:替换上面的通知和响应者方法。 See Apple's many examples of view controller autorotation, as well as their copious documentation, for how to do this. 有关如何执行此操作的信息,请参阅Apple的许多视图控制器自动旋转示例,以及它们的大量文档。

Third, if they're going to have a method respond to a notification, like in -orientationChanged: above, it should take an NSNotification object, not just a generic id. 第三,如果他们要有一种方法来响应通知,例如上面的-orientationChanged:它应该使用NSNotification对象,而不仅仅是通用ID。

I have tried so many of these alternatives, then I found out that you also have to be sure to change the variabel Initial interface orientation to what you want in addition to adding 我尝试了很多这样的选择,然后发现除了添加之外,您还必须确保将variabel Initial interface orientation更改为所需的Initial interface orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
}

somewhere in your implementation file. 实现文件中的某个位置。 Just the snippet worked in the beginning, but when adding more views and controllers, it all got messed up until I changed the .plist. 只是片段在开始就起作用,但是当添加更多视图和控制器时,直到我更改.plist为止,一切都弄糟了。

check out this post , it explains it well. 看看这篇文章 ,它解释得很好。

also, you can cast your orientations if needed. 另外,如果需要,您可以投射方向。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM