views:

325

answers:

1

Hello! I have trouble with auto-rotating interfaces in my iPad app. I have a class called Switcher that observes the interface rotation notifications and when it receives one, it switches the view in window, a bit like this:

- (void) orientationChanged: (NSNotification*) notice
{   
    UIDeviceOrientation newIO = [[UIDevice currentDevice] orientation];
    if (!UIDeviceOrientationIsValidInterfaceOrientation(newIO))
        return;
    UIViewController *newCtrl = /* something based on newIO */; 
    [currentController.view removeFromSuperview]; // remove the old view
    [window addSubview newCtrl.view];
    [self setCurrentController:newCtrl];
}

The problem is that the new view does not auto-rotate. My auto-rotation callback in the controller class looks like this:

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) io
{   
    NSString *modes[] = {@"unknown", @"portrait", @"portrait down",
        @"landscape left", @"landscape right"};
    NSLog(@"shouldAutorotateToInterfaceOrientation: %i (%@)", io, modes[io]);
    return YES;
}

But no matter how I rotate the device, I find the following in the log:

shouldAutorotateToInterfaceOrientation: 1 (portrait)
shouldAutorotateToInterfaceOrientation: 1 (portrait)

…and the willRotateToInterfaceOrientation:duration: does not get called at all. Now what? The orientation changing is becoming my least favourite part of the iPhone SDK… (I can’t check the code on the device yet, could it be a bug in the simulator?)


PS. The subscription code looks like this:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(orientationChanged:)
    name:UIDeviceOrientationDidChangeNotification
    object:nil];
A: 

The problem goes away when I repost the notification after exchanging the views:

[[NSNotificationCenter defaultCenter]
    postNotificationName:UIDeviceOrientationDidChangeNotification
    object:[UIDevice currentDevice]];

But boy, that sucks. I’d still welcome a better solution.

zoul