对UITabBarController和UINavigationController进行子类化

时间:2014-05-19 05:05:10

标签: ios xcode ios7 uinavigationcontroller uitabbarcontroller

您好我是iPhone开发的新手(使用iOS 7)。基本上我希望我的应用程序响应屏幕旋转(即更改屏幕方向)我的项目使用UITabBarControllerUINavigationController。但是,当我旋转设备时,它不会调用" shouldAutorotate"功能名为ViewController的{​​{1}}。

因此,我已按照here中的答案进行操作,直至将LoginView.mUITabBarController分类。有人可以向我解释如何在UINavigationController类或整个项目中添加(即引用它)方向行为。

非常感谢您的帮助。

谢谢

3 个答案:

答案 0 :(得分:0)

在子类UItabbarcontroller中

- (BOOL)shouldAutorotate {

    UINavigationController *navView = (UINavigationController *)[self selectedViewController];
    //Get the selected current tab viewcontroller. I guess you are having a Navigationcontroller    

    UIViewController *vc = [navView.viewControllers objectAtIndex:0];  
    //Fetch root viewcontroller of your navigationcontroller

    return [self checkOrientationForViewController:vc];

}

-(BOOL) checkForViewsForViewController : (UIViewController *)vc{

    if([vc isKindOfClass:[FirstViewController class]]){

        FirstViewController *vc1 = (FirstViewController *)vc;

        return [vc1 shouldAutorotate];
    }
    if([vc isKindOfClass:[SecondViewController class]]){

        SecondViewController *vc2 = (SecondViewController *)vc;

        return [vc2 shouldAutorotate];
    }
    if([vc isKindOfClass:[ThirdViewController class]]){

        ThirdViewController *vc3 = (ThirdViewController *)vc;

        return [vc3 shouldAutorotate];
    }
    return YES;
}

在各个viewcontrollers中实现shouldAutorotate方法

答案 1 :(得分:0)

而不是子类化,值得知道在UINavigationcontroller和UITabbarController中也有委托方法,它们可以让你在运行时处理轮换:

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController

Ref doc.

- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController

Ref doc

如果Apple已经删除了没有子类Nav和Tabbar控制器的建议,那么使用委托将是最可靠的解决方案

答案 2 :(得分:0)

您也可以使用

代替使用委托
- (NSUInteger)supportedInterfaceOrientations

UINavigationController和UITabBarController询问他们的子视图控制器它们支持哪种接口方向。所以你可以实现" - (NSUInteger)supportedInterfaceOrientations"在您的LoginView.m中并返回适当的界面方向。

您还需要编辑Info.plist并添加支持的界面方向。您可以通过打开项目视图使用Xcode执行此操作。另请查看Apples documentation on supporting interface orientations