【问题标题】:When Using UITabBarController, all view controllers receive shouldAutoRotate or just the displayed one?使用 UITabBarController 时,所有视图控制器都接收 shouldAutoRotate 还是只接收显示的?
【发布时间】:2011-07-28 11:32:11
【问题描述】:

如果我有一个 UITabBarController,显示 4 个视图控制器:vc1、vc2、vc3、vc4。
当显示 vc1 时,我旋转设备。
所有的视图控制器会接收并运行 AutoRotate 方法(shouldAutoRotate、willAutoRotate)还是只是 vc1?

如果只有 vc1:我应该只发送通知并为它注册所有 vc,以便它们也会轮换吗?因为我不想将 vc1 旋转到横向,然后转到 vc2,它仍然是横向的。

顺便说一句 - 如果重要的话 - 我为纵向和横向使用不同的 xib。

Tnx!

【问题讨论】:

    标签: uiviewcontroller notifications autorotate


    【解决方案1】:

    这取决于您的应用是如何构建的。你应该有某种 ViewController 来保存你的其他 viewController。所以你可以这样做:

    -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
        NSLog(@"Main will rotate");
    
        [view1 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        [view2 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        [view3 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        [view4 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    

    在这种情况下,您所有的 UIViewController 都会旋转,因为持有它们的人正在旋转并让他的孩子旋转。 :)

    我真的很喜欢这种方法,因为您不需要从每个 viewController 引用其他 viewController。 (view1不需要知道有view2、view3和view4)。

    【讨论】:

    • 该 VC 究竟是如何管理其他 VC 的?因为应该持有其他 VC 的 VC 实际上是选项卡栏......我也很难实现这一点,因为改变我的应用程序非常复杂,因为我已经建立了基本的 VC 结构及其现在相当复杂。我曾想过访问 self.tabBarController.viewControllers 之类的东西,但由于某种原因它不起作用。还有其他想法吗?
    • 编辑:我刚刚使这种方式起作用。我不知道为什么,但它不会发送 willRotateToInterfaceOrientation,但如果我调用一个我让自己旋转的方法,它会起作用。只需循环遍历 self.tabBarController.viewControllers 并将消息发送给所有 != self 的对象。挺好看的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多