自动旋转视图和UIDevice接口通知

时间:2010-05-06 14:01:07

标签: iphone ipad uiviewcontroller

我的iPad应用程序中的自动旋转界面有问题。我有一个名为Switcher的类,它观察接口旋转通知,当它收到一个时,它会在窗口中切换视图,如下所示:

- (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];
}

问题是新视图不会自动旋转。我在控制器类中的自动旋转回调如下所示:

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

但无论我如何旋转设备,我都会在日志中找到以下内容:

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

... willRotateToInterfaceOrientation:duration:根本没有被调用。怎么办?方向改变正成为我最不喜欢iPhone SDK的一部分...(我无法检查设备上的代码,它可能是模拟器中的错误吗?)


PS。订阅代码如下所示:

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

1 个答案:

答案 0 :(得分:0)

当我在交换视图后重新发布通知时,问题就消失了:

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

但男孩,那很糟糕。我仍然欢迎更好的解决方案。