【问题标题】:Trigger view controller in a navigation controller from tab bar controller从标签栏控制器触发导航控制器中的视图控制器
【发布时间】:2014-10-06 02:48:40
【问题描述】:

我有一个包含 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.

流程:

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)

【问题讨论】:

    标签: ios uinavigationcontroller uitabbarcontroller


    【解决方案1】:

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

    #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
    

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

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助。它有一个问题,因为它不是类型视图控制器而是 UINavigation 控制器。我错过了什么吗?我已经编辑了我的问题以显示我的故事板流程。感谢您迄今为止的所有帮助!
    • 你是对的。我已经更新了我的答案。不幸的是,我发现了另一个问题。这只会在第一次更改选项卡后起作用。因为要注册观察者,视图至少需要加载一次。我会努力解决这个问题,如果我有解决这个问题的办法,我会告诉你。
    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2017-05-14
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多