PDA

View Full Version : Why are rotation codes such a pain in the rear?




augeanst
Nov 7, 2009, 08:32 AM
I have been pulling my hair out for days, going down multiple blind paths; each no no end in sight. I am trying to do something that should be pretty simple. When the phone is in portrait mode I want UIView portrait to display. When in landscape I want UIView (yip you guessed it) landscape to load. Both of these UIViews are in the same nib file that is assoicated to my WERTYkeyboardForVIN class. Prior to adding any codes to switch views I am trying to just simply log to NSLog the current state (after that I will wrap my view codes in there).



I am sure I am screwing up something very simple (and stupid). Any thoughts or help would be greatly appreciated.



I am able to get shouldAutorotateToInterfaceOrientation to work:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(@"WERTYkeyboardForVIN shouldAutorotateToInterfaceOrientation");

if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
NSLog(@" - shouldAutorotateToInterfaceOrientation - UIInterfaceOrientationPortrait, returning YES");
return YES;
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(@" - shouldAutorotateToInterfaceOrientation - UIInterfaceOrientationLandscapeRight, returning YES");
return YES;
}
else
{
NSLog(@" - else - returning NO");
return NO;
}
}



As you would expect this outputs the following to the log:

WERTYkeyboardForVIN shouldAutorotateToInterfaceOrientation
- shouldAutorotateToInterfaceOrientation - UIInterfaceOrientationPortrait, returning YES



But when it comes to willAnimateRotationToInterfaceOrientation this method is never called and I am not sure why.


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"WERTYkeyboardForVIN - willAnimateRotationToInterfaceOrientation");
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
NSLog(@" - willAnimateRotationToInterfaceOrientation - UIInterfaceOrientationPortrait");
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(@" - willAnimateRotationToInterfaceOrientation - UIInterfaceOrientationLandscapeRight");
}
else
{
NSLog(@" - willAnimateRotationToInterfaceOrientation - else");
}
}



I *think* my problem is with a different view controller being 'in control' of the screen, but I do not know how to find out. Is there a debugging code I can type in gdb to find out the currently active view controller?



FYI here is how I am initializing WERTYkeyboardForVIN from ACMainMenu.m (I have also tried the codes in this class thinking since it is a view controller it might be in control, but nope...)


// Show WERTYkeyboardForVIN
if (wERTYkeyboardForVIN == nil)
{
WERTYkeyboardForVIN *controller = [[WERTYkeyboardForVIN alloc] initWithNibName: @"WERTYkeyboardForVIN" bundle:[NSBundle mainBundle]];
self.wERTYkeyboardForVIN = controller;
[controller release];

[appDelegate.window addSubview:[self.wERTYkeyboardForVIN view]];
}
else
{
[appDelegate.window bringSubviewToFront:[self.wERTYkeyboardForVIN view]];
}



Also I have tried placing my willAnimateRotationToInterfaceOrientation in the appDelegate to see if that was the problem, also tried it in my ACMainMenu (which is the primary view controller that is setup when the app launches).

thanks


Steven



wlh99
Nov 7, 2009, 11:10 PM
I had problems getting autorotation to work too. It really isn't hard, but I found that deviating from the documented examples didn't work the way I expected them too.

Try re-writing the code so that only one UIView is used, and that view knows how to re-arrange itself. You may have to pragmatically build the view if it is complex. If it isn't complex, you can set some basic rules for how the subviews will move on the size and position tab of the inspector in IB.

If you really need multiple views, try creating a navigation controller with no navigation items. Push whatever orientation you want on the navigation controller's stack.

PhoneyDeveloper
Nov 8, 2009, 10:53 AM
Apple's design is for one view controller to receive the various callbacks (viewWillAppear, etc.) at a time. If you try to have more than one view controller active at one time it won't work.

UINavController and UITabBar know how to connect the frontmost view controller so it receives the callbacks. If you use those classes and have only a single view controller active at a time then things should 'just work.'

Why it works this way and what the nav controller and tab bar do to make it work correctly just isn't known.