如何同时切换标签和pushViewController?

时间:2009-08-24 14:12:01

标签: iphone objective-c uitableview uitabbarcontroller

我的应用程序中有两个选项卡,左侧选项卡是普通的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:

试着解释每个标签上的内容:

Root (Tab Bar Controller)  
|  
--(Left Tab)-- UIViewController (I want to go from here...)  
|  
--(Right Tab)---UINavigationController  
             |  
             ---UITableViewController  
                 |  
                 ----UIViewController (...to here) 
                      ^ specific view with data from specific row
                        in table view (that is 'theView' from above)

2 个答案:

答案 0 :(得分:4)

对我来说,正确的方法是:

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

criscokid的答案有一个对navigationController的多次调用。 那是因为您的selectedViewController实际上是UINavigationViewController

答案 1 :(得分:1)

如果要切换标签栏并以同样的方法将ViewController推送到导航控制器堆栈,则不希望使用self.navigationController。如果我理解正确,您希望将其添加到右侧选项卡上的navigationController。我相信你会想要使用:

[[[[self tabBarController] selectedViewController] navigationController] pushViewController:theView animated:YES];