0

I wanted to make my project support full orientation. I'm on xcode 4.2 My implementation gives me one warning:

Warning

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?

Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
a3116b
  • 337
  • 1
  • 5
  • 13
  • possible duplicate of [Xcode: Getting warning "implicit coversion from enumeration type uideviceorientation"](http://stackoverflow.com/questions/7015709/xcode-getting-warning-implicit-coversion-from-enumeration-type-uideviceorienta) – Brad Larson Nov 03 '11 at 14:45

2 Answers2

3

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.

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. 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. 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:? That does pass in a UIInterfaceOrientation enum. I recommend replacing the notification and the responder method above with -didRotateFromInterfaceOrientation:. See Apple's many examples of view controller autorotation, as well as their copious documentation, for how to do this.

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • it's a little easy to say to go and see apple's exemples, most of them are up to date, and i just go to apple documentation to search about "view controller autorotation" nothing found thanks for your're very useful help – a3116b Nov 02 '11 at 16:01
  • @a3116b - Apple's documentation on this is relatively easy to find, such as the "Managing a View Controller's Interface Orientation" section of the View Controller Programming Guide for iOS: http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/BasicViewControllers/BasicViewControllers.html#//apple_ref/doc/uid/TP40007457-CH101-SW3 . Additionally, any new application you start from one of the standard templates automatically has these methods stubbed out for you. – Brad Larson Nov 02 '11 at 18:49
0

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

- (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.

chwi
  • 2,752
  • 2
  • 41
  • 66