iPhone / iOS:将Controller推送到当前未选中的NavigationController上

时间:2011-05-29 17:26:24

标签: iphone uinavigationcontroller uitabbarcontroller

在我的iPhone应用程序中,我使用了一个包含四个UINavigationController的UITabBarController。每个UINavigationController都代表我的应用程序中的特定导航。

通常,如果我想将新控制器推送到当前UINavigationController的堆栈中,我只需执行以下操作:

 // push another controller onto the current NavigationController Stack
 MyController* controllerToPush = [[MyController alloc]init];
 [self.navigationController pushViewController:controllerToPush animated:YES];
 [controllerToPush release];

但是我的应用程序中有一些地方,用户位于navigationController上,他的操作应该更改UITabBarController的选定Tab并将控制器推送到那里。我试过像:

// Get a reference to my appDelegate
MyAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];

// Change the selected Index of my TabBarController
[[appDelegate tabBarController] setSelectedIndex:0];

// Get a reference to the NavigationController whose Index within the TabBarController is 0
UINavigationController* navigationController = [appDelegate firstNavigationController];

// Push the newly initiliazed controller onto the navigationController
MyController* controllerToPush = [[MyController alloc]init];
[navigationController pushViewController:controllerToPush animated:YES];
[controllerToPush release];

但这种方法不起作用。 TabBar的selectedIndex可能正确,但controllerToPush尚未添加到navigationController-Stack中。我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试

[appDelegate.firstnavigationcontroller pushViewController:controllerToPush animated:YES];