0
votes

I have a UINavigationController as the root view controller of an app.

At some point I need to push a UITabController with specific tabs.

Up to this point it all works.

However in the tab view controllers I can't access the UINavigationController navBar (to change title, tint, etc).

Here is how I push it:

SCTabController *TabController = [[SCTabController alloc] init];
[self.navigationController pushViewController:TabController animated:YES];

SCTabController is a subclass of UITabController with the following viewDidLoadMethod:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIViewController *view1 = [[UIViewController alloc] init];
    UIViewController *view2 = [[UIViewController alloc] init];
    UIViewController *view3 = [[UIViewController alloc] init];
    UIViewController *view4 = [[UIViewController alloc] init];

    NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
    [tabViewControllers addObject:view1];
    [tabViewControllers addObject:view2];
    [tabViewControllers addObject:view3];
    [tabViewControllers addObject:view4];

    [self setViewControllers:tabViewControllers];
    //can't set this until after its added to the tab bar
    bankHomeViewController.tabBarItem =
            [[UITabBarItem alloc] initWithTitle:@"Title1"
                                          image:nil
                                            tag:1];
    view2.tabBarItem =
            [[UITabBarItem alloc] initWithTitle:@"Title2"
                                          image:nil
                                            tag:2];
    view3.tabBarItem =
            [[UITabBarItem alloc] initWithTitle:@"Title3"
                                          image:nil
                                            tag:3];
    view4.tabBarItem =
            [[UITabBarItem alloc] initWithTitle:@"Title4"
                                          image:nil
                                            tag:4];
2

2 Answers

0
votes

In my project I have done so : enter image description here

0
votes

add UITabBarControllerDelegate

@interface SCTabController : UITabBarController<UITabBarControllerDelegate>

@end

then add below code in viewDidLoad in SCTabController.m

self.delegate = self;

implement UITabBarControllerDelegate didSelectViewController in SCTabController.m

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //Write your code here
    //NSLog(@"%lu", (unsigned long)self.selectedIndex);

    UILabel *functionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 190, 50)];
    [functionTitleLabel setTextAlignment:NSTextAlignmentCenter];

    switch (self.selectedIndex) {
        case 0:
            [functionTitleLabel setText:@"title1"];
            break;
        case 1:
            [functionTitleLabel setText:@"title2"];
            break;
        case 2:
            [functionTitleLabel setText:@"title3"];
            break;
        default:
            [functionTitleLabel setText:@"title4"];
            break;
    }

    self.navigationItem.titleView = functionTitleLabel;
}