如何从 Tabbar 控制器获取导航控制器并推送特定视图

How to get UINavigation controller from Tabbar controller and push a specific view

我正在尝试实施 didReceiveRemoteNotification 方法。当我收到推送通知时,我想显示一个特定的视图。我的应用使用 UITabbarController 作为根视图控制器,使用 UINavigationController 作为它的子视图:

这是我的 appdelegate:

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        UINavigationController *navController = (UINavigationController *)[[((UITabBarController *)self.window.rootViewController) viewControllers] objectAtIndex:2];
        NSString * storyboardName = @"others";
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessAccountView"];
        [navController pushViewController:vc animated:YES];
        [PFPush handlePush:userInfo];
}

不幸的是,代码不起作用。如果我将 [navController pushViewController:vc animated:YES] 更改为 [navController presentViewController:vc animated:YES],它会起作用,但我会丢失导航栏。 有谁知道如何做到这一点?

遵循本教程(故事板教程:创建选项卡栏控制器和 Web 视图)....

http://www.appcoda.com/storyboard-tutorial-create-tab-bar-controller-and-web-view/

这是一个逐步解决方案。

  • 首先检查要推送视图控制器的 选项卡 是否已经 selected。如果没有,请先使用 setSelectedIndexsetSelectedViewController 到 select 该选项卡。
  • 接下来,获取 tabBarController 的 selected 视图控制器的 rootViewController(您的 UINavigationController)。
  • 接下来实例化你要推送的viewController,推送到navController上

示例代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UITabBarController* tabController = self.window.rootViewController;
    if (tabController.selectedIndex != 2) [tabController setSelectedIndex:2];



    UINavigationController *navController = tabController.selectedViewController;
    NSString * storyboardName = @"others";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessAccountView"];
    [navController pushViewController:vc animated:YES];
    [PFPush handlePush:userInfo];
}