Custom UITabBarController and UINavigationControll

2020-02-15 12:00发布

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).

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

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

4条回答
Bombasti
2楼-- · 2020-02-15 12:43

Suppose you have a tabbarController. Now you can add any viewController or any NavController in your tabController. NavController can contain viewController. But you might have confusion where you will use navController or viewController. You will use viewController where you don't need navigation, I mean where you don't need.

Here is an code example where the first view contains only view and the second view contains navigation controller. You can't push new view in first view, but you can easily push new view in second view.

-(void)addTabBarControllers
{
    UIViewController *viewController1, *viewController2;

    viewController1 = [[[HomeView alloc] initWithNibName:@"HomeView" bundle:nil] autorelease];
    viewController2 = [[[FloorPlanHome alloc] initWithNibName:@"FloorPlanHome" bundle:nil] autorelease];


    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, nav2, nil];

    [[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"First View"];
    [[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Second View"];


    [[self.tabBarController.tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:@"first.png"]];
    [[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"second.png"]];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
}

Call this method from didFinishLaunchingWithOptions in AppDelegate. here HomeView and FloorPlanView is two different views, you need to add these views and class file first.

查看更多
Bombasti
3楼-- · 2020-02-15 12:45

Should I create a separate UINavigationController for each tab

If you want to navigate in each tab, YES you should add each viewController embedded in a navigationController.

查看更多
贪生不怕死
4楼-- · 2020-02-15 12:51

Refer to my answer here:
UITabBarController Issue

if(!self.tabBarController)
    self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController.delegate=self;

NSMutableArray *localcontrollerarray = [[NSMutableArray alloc] initWithCapacity:2];

UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

UINavigationController *navi1 = [[UINavigationController alloc] initWithRootViewController:viewController1];

[localcontrollerarray addObject:navi1];

UIViewController *viewController2 = [[ScanViewController alloc] initWithNibName:@"ScanViewController" bundle:nil];

UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:viewController2];

[localcontrollerarray addObject:navi2];

self.tabBarController.viewControllers = localcontrollerarray;

[self.window addSubview:self.tabBarController.view];
查看更多
做自己的国王
5楼-- · 2020-02-15 13:03

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

and this in you AppDelegate.m:

self.window.rootViewController = self.yourUITabBarController;
查看更多
登录 后发表回答