【问题标题】:Back to home page tabbar from deep level viewcontroller从深层视图控制器返回主页选项卡栏
【发布时间】:2018-01-24 13:57:15
【问题描述】:

这是我的标签栏。在我点击“啤酒”后,将重定向到下一个 VC[!

现在,我点击“华为...”项目

在这里,我点击右上角的更多按钮,最后会弹出带有主页按钮的菜单。

我有一个弹出视图控制器,如下所示:-

当我点击主页按钮(第一个按钮)时,我将调用如下点击函数:-

- (void)toolsButtonClick1:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"click home - index 0");
    int index = 0;
    self.tabBarController.selectedIndex = index;
    [self.tabBarController.viewControllers[index] popToRootViewControllerAnimated:YES];

    NSLog(@"click home - index 1");
}

关闭视图控制器工作正常,我可以收到 NSLog 但不会重定向到标签栏控制器。谢谢。

【问题讨论】:

  • 我认为你会得到所有时间索引 0,你面临这个问题而不是使用标签属性
  • 您好 JRB,如果我在其他模块中使用这些代码“[self.tabBarController.viewControllers[index] popToRootViewControllerAnimated...”,我可以将其链接回我的主页。但是,此视图控制器中的结果将隐藏弹出视图并保持不变。我期望的是返回主页的链接。
  • 您正在从当前的ViewController 调用tabBarController,但不在tabBarController 中,因此您必须将委托放在您获得解除方法的位置,然后从那里调用self.tabBarController.selectedIndex
  • 嗨 vp2698,你能在这里给我看一些代码示例吗?非常感谢您的帮助。

标签: ios objective-c


【解决方案1】:

您可以使用NSNotificationCenter 更改tabBar 的索引

//From where you presenting viewController
// In viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"ChangeIndex" object:nil];

可以更改索引的方法

-(void) receiveNotification:(NSNotification*)notification
{
    if ([notification.name isEqualToString:@"ChangeIndex"])
    {
       NSDictionary* userInfo = notification.userInfo;
       NSNumber* index = (NSNumber*)userInfo[@"indexToChange"];
       self.tabBarController.selectedIndex = index.intValue;
     }
}

还有你的方法

- (void)toolsButtonClick1:(UIButton *)button
{ 

   int index = 0;
   NSDictionary* userInfo = @{@"indexToChange": @(index)};

   NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
   [nc postNotificationName:@"ChangeIndex" object:self userInfo:userInfo];
   [self dismissViewControllerAnimated:YES completion:nil];
}

【讨论】:

  • 您好 vp2698,谢谢您的解释。但是,在我的项目中应用上述代码后,我仍然没有运气。我将“[[NSNotificationCenter defaultCenter]...”和 2 个方法放在我的 ViewController 中,但仍然相同。有什么想法吗?
  • 您的 viewcontroller 是否在标签栏中,您在此处展示此视图,如图所示?
  • 此视图控制器不是来自标签栏。来自深层视图控制器。我想回到标签栏(主页)。
【解决方案2】:

我认为您的问题可能是因为您尝试在更改标签栏 VC 的同时关闭视图控制器。您可以通过将其中一个放入另一个的完成块来序列化它。

- (void)toolsButtonClick1:(UIButton *)button
{
    NSLog(@"click home - index 0");

    // Get presenting VC here because it will be gone in the completion handler
    UIViewController *presentingVC = self.presentingViewController;

    [self dismissViewControllerAnimated:YES completion:^{
        int index = 0;
        presentingVC.tabBarController.selectedIndex = index;
        [presentingVC.tabBarController.viewControllers[index] popToRootViewControllerAnimated:YES];

        NSLog(@"click home - index 1");
    }];
}

【讨论】:

  • 您好 LGP,不走运,在我的代码中应用后仍然保持不变。
  • 也许问题是您当前的 VC 是模态的,因此没有正确的 tabBarController?在这种情况下,您需要从父 VC 获取 tabBarController。我已经为此更新了代码。
  • 您好 LGP,还是一样。我的 VC 是 tabbarcontroller [index 1] 下的 2 个子级别。现在我点击的水只会消失弹出菜单(如上图所示)。
  • 是不是说明你已经做了两层presentViewController?在这种情况下,您还需要解雇两次。
  • 你的意思是我需要把 [self dismissViewControllerAnimated:YES completion:nil];两次?
猜你喜欢
  • 2016-12-11
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
相关资源
最近更新 更多