views:

4511

answers:

6

My app launches in landscape mode correctly and works great:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait)
        return NO;
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft )
        return YES; 
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

And updated Info.plist with

UIInterfaceOrientation = UIInterfaceOrientationLandscapeRight

Now in my main controller I switch out one ViewController for another

characterController = [ [ CharacterController alloc ] init];
myCurrentViewController = characterController;
self.view =  myCurrentViewController.view ;

and it loads but the orientation is in Portrait mode. If I then rotated the iPhone it corrects it to landscape mode. Any ideas how to keep landscape orientation when loading a new viewController into my mainController?

A: 

I think you need to implement

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

in your CharacterController as well.

Dean Smith
It is implemented the question did not reflect that. I updated the question
Charles Peterson
A: 

Once again I found my own answer. LOL

After loading the new ViewController

self.view =  myCurrentViewController.view;

Update controller to orientate the new ViewController to landscape

self.view.transform = CGAffineTransformMakeRotation(-3.14 * (90) / 180.0);
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.center = CGPointMake(160.0f, 240.0f);
Charles Peterson
A: 

Found another solution:

[myCurrentViewController presentModalViewController: newController animated: NO];

Charles Peterson
+5  A: 

Be very careful about your self.view=otherVC.view approaches. A UIViewController is intended to manage a single view. It's not designed to have its view swapped out (which is why your orientation changes aren't working). This matters in cases like -didReceiveMemoryWarning if your ViewController isn't on the screen. It will quietly dump its view, and when it comes back on screen, reload the view from the NIB (or re-run -loadView).

Your presentModalViewController: approach is somewhat better, though it's not how a modal view is built to work. It at least lets each ViewController manage its own view. Typically you would use a UITabBarController or UINavigationController here. I assume you have some reason you're avoiding these.

My recommended solution to this would be to add a UIView to your main view controller's view (as an IBOutlet or in code). You swap that view in and out rather than swapping the UIViewController's view in and out. I'd probably go ahead and subclass UIViewController to handle this, with methods modeled after UITabBarController.

@interface RNSwappableViewController : UIViewController
{
    ...
}
@property(nonatomic, assign) id<RNSwappableViewControllerDelegate> delegate;
@property(nonatomic) NSUInteger selectedIndex;
@property(nonatomic, assign) UIViewController *selectedViewController
@property(nonatomic, copy) NSArray *viewControllers;
@end

@protocol RNSwappableViewControllerDelegate : NSObject
- (void)swappableViewController:(RNSwappableViewController *)swappableViewController didSelectViewController:(UIViewController *)viewController
@end
Rob Napier
Yes after I fixed my problem and read up even more I found out that UINavigationController should have been used.
Charles Peterson
A: 

Found an even better solution. LOL. I'm developing a game and decided to use Cocos2D. Why didn't I do this before. Cocos2D all the way!!!!!!

Charles Peterson
this isn't a valid solution
Sneakyness
A: 

Did you have the problem where shouldAutorotateToInterfaceOrientation got called with UIInterfaceOrientationPortrait starting out even when phone was landscape?

I have an app that's primarily portrait-only and with one view controller I bring up with presentModalViewController that I do want to autorotate, which I got working well, but when I was opening it it would always start in portrait. The view controller seemed to be preferring the parent view controller's orientation over the device's.

This is what worked for me, in the modal view's view controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

// if device orientation is one of these, don't care what our interface orientation is
if (deviceOrientation == UIDeviceOrientationUnknown && deviceOrientation == UIDeviceOrientationFaceUp && deviceOrientation == UIDeviceOrientationFaceDown)
  return YES;

// otherwise only return YES for the ui orientation matching the current device orientation
return (interfaceOrientation == deviceOrientation);
}
smallduck