【问题标题】:how to present a navigation controller embedded in a tab bar controller when receiving a push notification如何在收到推送通知时呈现嵌入在标签栏控制器中的导航控制器
【发布时间】:2014-04-28 17:00:18
【问题描述】:

我正在开发一个带有标签栏控制器和推送通知的项目,当我的应用根据警报正文收到推送通知时,它将打开一个嵌入标签栏的确定视图控制器。

我已经可以展示视图控制器,但我丢失了标签栏。

为了呈现视图控制器,这就是我所做的:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            vc5 *ivc = [storyboard instantiateViewControllerWithIdentifier:@"vc5"];
            UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:ivc];
            self.window.rootViewController =nil;
            self.window.rootViewController = navigationController;
            [self.window makeKeyAndVisible];

我尝试了这段代码来显示标签栏,但我没有工作:

UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:@"tabBarController" bundle:nil];
    tabBarController.selectedViewController = [storyboard instantiateViewControllerWithIdentifier:@"tabBarController"];
    tabBarController.selectedViewController = nil;
    tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0];
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    [tabBar setSelectedIndex:0];

我的 Storyboard 包含一个位于导航控制器内的 Initial Login ViewController,在用户登录后提示选择一个城市,然后我呈现一个包含 o 4 个选项卡的选项卡栏控制器,我需要通过推送通知位于第一个选项卡中,是该导航控制器内的第五个视图。

我在 SO 中查看了其他问题,但没有任何效果,这是我第一次在 iOS 中开发,任何帮助将不胜感激。谢谢


编辑: 当我收到通知时,我也尝试从 AppDelegate 进行 segue,但我收到此错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<TabBarController: 0x16dec150>) has no segue with identifier 'pushNotification'

这是我的推送代码:

TabBarController *tabBar = [[TabBarController alloc] initWithNibName:@"tabBarController" bundle:nil];
    [tabBar performSegueWithIdentifier:@"pushNotification" sender:self];

【问题讨论】:

  • 如果您使用的是故事板,为什么您要尝试从代码加载 tabbarcontroller。 segue 会为你做的。
  • @pawan 看到我编辑的问题,我也尝试从 AppDelegate 执行 segue,但我认为这是不可能的,或者我无法让它工作,谢谢
  • 你找到答案了吗?
  • 你找到答案了吗?
  • @ChandanAnand 我最终将它呈现为模态,产品要求发生了变化

标签: ios objective-c uitabbarcontroller apple-push-notifications


【解决方案1】:

从初始登录视图控制器执行 segue,而不是从应用程序委托。为了使用情节提要进行推送转场,您必须命名转场并在情节提要中查看。您可以通过选择它们并使用 xCode 右侧的属性面板来命名这些项目。

【讨论】:

    【解决方案2】:
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
    //Create view controller for Main here
    UIViewController * MainNC = [storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
    
    [self presentViewController:MainNC animated:NO completion:nil];
    

    【讨论】:

      【解决方案3】:

      我通过在通知到达时显示视图控制器并将必要的数据传递给视图控制器来解决这个问题

      【讨论】:

      • 这听起来很糟糕。你为什么不直接调用你想要的导航控制器呢?
      • @mcphersonjr 我更新了我的答案,我最终做了什么
      • 这个答案不应该被接受,因为它不适合原来的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多