Rich Allen
Rich Allen

Reputation: 59

Trigger view controller in a navigation controller from tab bar controller

I have an app that has a tab bar controller that houses 5 navigation controllers. Each nav controller has a menu as the first view controller however when the tab is selected I want to bypass the menu and go to the next view controller.

Flow:

TabBar Controller -> Nav Controller -> Menu -> View Controller 1 -> View Controller (level 2)

              -> Nav Controller -> Menu -> View Controller 2 -> View Controller (level 2)

              -> Nav Controller -> Menu -> View Controller 3 -> View Controller (level 2)

              -> Nav Controller -> Menu -> View Controller 4 -> View Controller (level 2)

Upvotes: 1

Views: 653

Answers (1)

riik
riik

Reputation: 4448

You could use the NSNotificationCenter to post notifications to your View Controllers when the Tab Bar gets tapped. To set up this mechanism you have to do this in your first view Controller.

#import "FirstViewController.h"

@interface FirstViewController ()<UITabBarControllerDelegate>

@end

@implementation FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tabBarController.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (((UINavigationController *)viewController).topViewController == self) {

        //Push the next view controller to skip the menu
        //Replace newViewController with the disired one
        UIViewController *newViewController = [UIViewController new];
        [self.navigationController pushViewController:newViewController animated:NO];

        NSLog(@"Push view controller first tab");

    }else {
        //Post a notification to inform the other view Controller
        [[NSNotificationCenter defaultCenter]postNotificationName:@"tabBarControllerDidSelectViewController" object:((UINavigationController *)viewController)];
    }
}


@end

In your the rest of your view controllers do the following

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didTabed:) name:@"tabBarControllerDidSelectViewController" object:nil];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.     }

-(void)didTabed:(NSNotification *)notification
{
    if ([[notification object] isKindOfClass:[UINavigationController class]]) {

        UIViewController *selectedViewController = ((UINavigationController *)[notification object]).topViewController;

        if (selectedViewController == self) {

            //Push the next view controller to skip the menu
            //Replace newViewController with the disired one
            UIViewController *newViewController = [UIViewController new];
            [self.navigationController pushViewController:newViewController animated:NO];

            NSLog(@"Push view Controller tap2");
        }

   }
}

@end

If you have any questions about this please don't hesitate to ask!

Hope this helps.

Upvotes: 0

Related Questions