如何同时切换选项卡和推出视图控制器?

3

我的应用程序中有两个选项卡,左边的选项卡是普通的UIViewController,右边的选项卡是一个包含表格视图的导航控制器。我希望允许用户在左侧选项卡上按下一个按钮,并跳转到右侧选项卡中表格视图中特定行所属的具体详细信息视图。

在表格视图本身中,我没有问题显示特定的详细信息视图,例如:

[self.navigationController pushViewController:theView animated:YES];

我可以使用以下方法来切换标签页:

self.tabBarController.selectedIndex = 1; // 1= right tab

但我不知道如何同时做到这两点。我已经尝试过。
self.tabBarController.selectedIndex = 1; // 1= right tab
// alloc and init theView here
[self.navigationController pushViewController:theView animated:YES];

但这并不起作用。

编辑:

当我尝试上面的代码时,它只是切换了选项卡,但没有推送视图。

我也尝试了criscokid建议的方法,但什么都没有发生。

编辑2:

试图解释每个选项卡上的内容:

根(选项卡控制器)
|
--(左选项卡)--UIViewController(我想从这里去...)
|
--(右选项卡)---UINavigationController
             |
             ---UITableViewController
                 |
                 ----UIViewController(...到这里)
                      ^具有特定行中数据的特定视图
                        在表格视图中(即上面的“theView”)
2个回答

4

对于我而言,正确的方法是:

 UINavigationController * navigationController = (UINavigationController *) [[self tabBarController] selectedViewController];
 [navigationController pushViewController:viewController animated:YES];

criscokid的回答中有一个多余的调用navigationController。 这是因为您的selectedViewController实际上是一个UINavigationViewController


我在重复调用方面犯了同样的错误。这个方法解决了我的问题。 - Trevor Gehman

1
如果您正在切换选项卡栏并将ViewController推入导航控制器堆栈中的同一方法中,则不应使用self.navigationController。如果我理解正确,您想将其添加到右侧选项卡上的导航控制器。我认为您应该使用:
[[[[self tabBarController] selectedViewController] navigationController] pushViewController:theView animated:YES];

这对我不起作用。如果我正确理解你的代码,你在导航控制器上调用了pushview,这应该是当前选定视图控制器在选项卡栏中的父级。然而,由于它只在右侧选项卡上,所以没有父级导航控制器。 - James
你能再试着解释一下每个标签页上是什么吗? - criscokid

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