2014-03-05 54 views
2

使用UITabBarControllerDelegate方法:UITabViewController:如何獲取正確的可見標籤欄(selectedViewController顯示錯誤結果)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ 
// I want to check the currently displayed view controller and the selected one to decide what to do 
if (self.selectedViewController==self.viewController1){ 
    if(viewController==self.viewController2 || viewController==self.viewController3){ 
     // do stuff 
    } 
} 

基本上我檢查,如果被從當viewController1可見按下不同的選項卡,做東西。據Apple's documentation

@屬性(非原子,分配)的UIViewController * selectedViewController

討論

此視圖控制器是其自定義視圖當前由選項卡欄界面中顯示的一個。

現在爲奇怪: 在我的代碼中,你可以看到我引用self.selectedViewController來檢查當前顯示的一個,它始終返回與傳遞的ViewController參數完全相同,但didSelect。也許self.selectedViewController已經被設置爲didSelect的結果將顯示的那個?如果是這樣,我應該怎樣或在哪裏獲得之前 didSelect?我需要根據當前顯示的正確路徑 - >新選項卡編寫一些核心數據。

回答

1

使用tabBartController:shouldSelectViewController:委託方法。

+0

這工作完美。謝謝 – LMVogel