在
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
做
[viewController.navigationController popToRootViewControllerAnimated:YES];
將在選項卡的視圖控制器導航到每當選擇的標籤根視圖。
在應用程序委託中保持主窗口視圖控制器(標籤欄控制器)實例完全可以。
你需要添加什麼是設置委託或任何其他初始化類是標籤欄控制器的委託是這樣的:
myTabBarController = [UITabBarController alloc ...
myTabBarController.delegate = self; // the app delegate will be also the tab bar delegate
在應用程序委託
,那麼你將以下方法添加到應用程序代表:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[viewController.navigationController popToRootViewControllerAnimated:YES];
}
選中標籤欄時,標籤欄控制器將調用此方法。 你也想使應用程序委託確認UITabBarControllerDelegate
這樣:
@interface PSAppDelegate : UIResponder <UITabBarControllerDelegate>
,可以讓編譯器知道,應用程序的委託必須或有可能在協議中聲明的方法,也將讓你如此方便自動這種方法的比較。
它什麼都不做,但也許我需要定義哪個視圖是根視圖?如果是的話 - 我該怎麼做?附:'popToRootViewController'未定義,我使用'popToRootViewControllerAnimated:(BOOL)' – cyborg86pl 2012-07-12 14:55:49
@ cyborg86pl感謝您的編輯。你確定你有'UINavigationController'作爲選項卡控制器嗎? 'didSelectViewController'方法必須放在標籤欄的委託類中,檢查調用的方法。 'navigationController'屬性爲您提供了視圖控制器推送到的導航控制器,即如果使用標籤欄委託並且您有'UINavigationController'選項卡控制器,則代碼應該工作得很好。 – 2012-07-12 15:04:28
我是iOS新手,所以看起來我的問題位於我的代碼更深處。我以編程方式在我的AppDelegate中放入了'UITabBarController'。我是否需要將代碼放在AppDelegate實現中或其他位置?我現在很困惑...還試過[that](http://stackoverflow.com/questions/4487718/how-do-i-integrate-poptorootviewcontrolleranimated-with-my-tabs?answertab=active#tab-top )但它仍然不夠... – cyborg86pl 2012-07-12 16:03:57