简体   繁体   English

从选项卡栏控制器触发导航控制器中的视图控制器

[英]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. 我有一个带有标签栏控制器的应用程序,其中包含5个导航控制器。 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) TabBar控制器->导航控制器->菜单-> 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)

You could use the NSNotificationCenter to post notifications to your View Controllers when the Tab Bar gets tapped. 当标签栏被点击时,您可以使用NSNotificationCenter将通知发布到视图控制器。 To set up this mechanism you have to do this in your first view Controller. 要设置此机制,您必须在第一个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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从导航栏视图控制器导航到选项卡栏视图控制器 - Navigate from a navigation bar view controller to a tab bar view controller 从标签栏按钮将视图控制器推到导航控制器上 - Pushing a view controller onto a navigation controller from a tab bar button 具有导航控制器的标签栏控制器的导航控制器 - Navigation controller with tab bar controller of navigation controller 导航控制器到选项卡栏控制器 - Navigation controller to tab bar controller 带标签栏控制器的导航控制器? - Navigation controller with tab bar controller? 导航 Controller 和标签栏 Controller - Navigation Controller and Tab Bar Controller 为什么将视图控制器嵌入导航控制器,然后在选项卡栏控制器中将导航栏附加到选项卡栏控制器? - Why does embedding a View Controller in a Navigation Controller, then in a Tab Bar Controller attach the nav bar to the Tab Bar Controller? 如何从嵌入在导航控制器中的简单视图控制器调用标签栏视图控制器? - How to call tab bar view controller from simple view controller which is embedded in navigation controller? 从视图控制器移动到标签栏控制器 - Move from view controller to tab bar controller 旧的导航栏和标签栏控制器的视图控制器问题 - View controller problems with old navigation bar and tab bar controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM