切换选项卡栏以在导航到另一个视图控制器之前。

11

我正在开发一款iOS应用,其中我有选项卡栏+侧边菜单

选项卡栏有5个项目,而侧边菜单大约有12个菜单。

所有侧边菜单功能来自选项卡1,侧边菜单可以在选项卡栏的所有视图中访问。

这意味着,如果我在选项卡2上,我也可以访问侧边菜单。当我从选项卡1的侧边菜单中点击菜单项时,我将进入选项卡1,然后进行导航。

我想做的是,比如说,如果我点击侧边菜单中的Complains菜单,我想要进入ComplainsViewController。

我使用的代码如下。

// go to first tab
self.tabBarController.selectedIndex = 0;
// now navigate
ComplainsViewController *sViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Complains"];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:sViewCon animated:NO];

我有两种情况。

情况1(正确)

我正在选项卡1上,然后从侧边菜单中选择“投诉”(Complains)。当我点击时,使用上述代码成功跳转到ComplainsViewController。

情况2(不正确)

我正在选项卡2上,然后从侧边菜单中选择“投诉”(Complains)。当我点击时,我成功地转到选项卡1,但我没有导航到ComplainsViewController。当我返回选项卡2时,我看到ComplainsViewController在选项卡2中打开。

你有什么想法如何先切换到选项卡,然后再导航到另一个视图控制器?


编辑1

下面是我的基本结构。

enter image description here


你尝试过在推出视图控制器之前弹出根视图控制器吗?[[self navigationController] popToRootViewControllerAnimated:NO]; - oyvindhauge
2个回答

3
最后我用NSNotificationCenter的方法解决了这个问题。

以下是我的方法:

第一步(在 SideMenuViewController 中):

// let first go to first index
self.tabBarController.selectedIndex = 0; 
// now post notification and make transition
[[NSNotificationCenter defaultCenter] postNotificationName:@"TakeMeToComplains" object:nil];

步骤一,将更改selectedIndex并发布通知。

步骤二(在所有其他视图控制器中)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(globalTakeMeToComplains) name:@"TakeMeToComplains" object:nil];

 -(void)globalTakeMeToComplains {
      // here I have code for Transition to go to ComplainsViewController
 }

在第二步中,当我们切换到第一个选项卡时,将调用viewWillAppear函数,我会监听通知。一旦接收到通知,我将执行转换到另一个视图控制器的操作。
如果有人不清楚我的操作,请告诉我。

3

看起来你在切换标签后忘记切换navigationController了。 尝试实现UITabBarControllerDelegate方法:

- (void)tabBarController:(UITabBarController * _Nonnull)tabBarController
 didSelectViewController:(UIViewController * _Nonnull)viewController {
    self.currentNavigationController = (UINavigationController *)viewController;
}

如果你想手动切换选项卡,请使用以下代码:

// go to first tab
self.tabBarController.selectedIndex = 0;

// Manually call delegate method
[self tabBarController:self.tabBarController didSelectViewController:self.tabBarController.selectedViewController];

// now navigate
ComplainsViewController *sViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Complains"];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.currentNavigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.currentNavigationController pushViewController:sViewCon animated:NO];

为了实现这个目标,您需要创建一个新属性来保存currentNavigationController

你能详细说明一下你的话吗?针对这个目标,你需要创建一个新的属性来保存当前导航控制器。我不明白需要做什么... - Fahim Parkar
我正在谈论在类中创建属性:@property(nonatomic,strong)UINavigationController *currentNavigationController; - Vlad Papko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接