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

时间:2015-04-10 08:31:50

标签: ios objective-c uitabbarcontroller uinavigationbar

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

storyboard

这是我的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],它会有效,但我会丢失导航栏。 有没有人知道如何做到这一点?

2 个答案:

答案 0 :(得分:0)

按照本教程(故事板教程:创建标签栏控制器和Web视图)....

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

答案 1 :(得分:0)

这是一个逐步的解决方案。

  • 首先检查是否已选择要推送视图控制器的选项卡。如果没有,请先使用setSelectedIndexsetSelectedViewController选择该标签。
  • 接下来,获取tabBarController所选视图控制器的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];
}