自定义UITabBarController和UINavigationController [英] Custom UITabBarController and UINavigationController

查看:139
本文介绍了自定义UITabBarController和UINavigationController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发iOS5及以上版本的应用程式,我不使用故事板或IB。我创建了一个自定义 UITabBarController 和在我的 AppDelegate 我把它4个视图控制器只有1 UINavigationController (不能告诉为什么)。

I'm developing an app for iOS5 and up and I don't use storyboards or IB. I'm creating a custom UITabBarController and in my AppDelegate I'm putting in it 4 view controllers with only 1 UINavigationController (can't tell why).

这会导致一个行为,其中我只能从首先选项卡推送新的VC,这显然是打包成 UINavigationController navController

It results in a behaviour where I can push new VC only from the first tab, which is apparently, packed into a UINavigationController called navController:

SGTabBarController *tabBarController = [[SGTabBarController alloc] init];

    SGHomeViewController* vc1 = [[SGHomeViewController alloc] init];
    SGChooseOSAgainViewController* vc3 = [[SGChooseOSAgainViewController alloc] init];
    SGSmsServicesViewController* vc4 = [[SGSmsServicesViewController alloc] init];
    SGSupportViewController *vc5 = [[SGSupportViewController alloc] init];
    navController = [[UINavigationController alloc] initWithRootViewController:vc1];

    NSArray* controllers = [NSArray arrayWithObjects:navController, vc3, vc4, vc5, nil];
    tabBarController.viewControllers = controllers;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = tabBarController;
    [navController setNavigationBarHidden:YES animated:NO];
    [self.window makeKeyAndVisible];

为什么?我应该为每个标签创建一个单独的 UINavigationController ?我从Apple的文档中获取了这些代码。

Why is that? Should I create a separate UINavigationController for each tab? I took this code from Apple's documentation.

推荐答案

是的,可以。在你的UITabBarController.m中尝试类似下面的代码

Yes, you can. Try something like this code in yourUITabBarController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    NSMutableArray* sectionViewControllers = nil;
    NSArray* controllers = [self controllers];
    UIViewController* controller = nil;
    
    for (controller in controllers)
    {
        if (sectionViewControllers == nil)
            sectionViewControllers = [NSMutableArray arrayWithCapacity:0];
        
        UINavigationController* navigationController = [[UINavigationController allocWithZone:[self zone]] initWithRootViewController:controller];
        
        navigationController.navigationBarHidden = YES;
        
        [sectionViewControllers addObject:navigationController];
        [navigationController release];
    }
    
    self.viewControllers = sectionViewControllers;
}

- (NSArray*)controllers
{
    if (!_controllers)
        _controllers = [NSArray arrayWithObjects:[self tabController1], [self tabController2], nil];
    return _controllers;
}

这在AppDelegate.m中:

and this in you AppDelegate.m:

self.window.rootViewController = self.yourUITabBarController;

这篇关于自定义UITabBarController和UINavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆