从标签栏控制器触发导航控制器中的视图控制器

时间:2014-08-12 21:30:35

标签: ios uinavigationcontroller uitabbarcontroller

我有一个应用程序,它有一个标签栏控制器,可容纳5个导航控制器。每个导航控制器都有一个菜单作为第一个视图控制器,但是当选择选项卡时,我想绕过菜单并转到下一个视图控制器。

流速:

TabBar控制器 - >导航控制器 - >菜单 - >视图控制器1 - >查看控制器(级别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)

1 个答案:

答案 0 :(得分:0)

当标签栏被点击时,您可以使用NSNotificationCenter向View Controllers发布通知。要设置此机制,您必须在第一个视图控制器中执行此操作。

#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

在您的其余视图控制器中执行以下操作

#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

如果您对此有任何疑问,请不要犹豫!

希望这会有所帮助。