Reputation: 337
I wanted to make my project support full orientation. I'm on xcode 4.2 My implementation gives me one 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?
Upvotes: 0
Views: 4054
Reputation: 2802
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.
Upvotes: 0
Reputation: 170319
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.
Upvotes: 3