首先,如Apple文檔中所述,選項卡欄應位於根級別,即添加到窗口。這是正確行爲的關鍵。
其次,你可以使用UITabBarDelegate
/UINavigationBarDelegate
轉發手動的通知,但我發現,要得到從整體層次要求正常工作,我所要做的就是手動調用
[tabBarController viewWillAppear:NO];
[tabBarController viewDidAppear:NO];
和
[navBarController viewWillAppear:NO];
[navBarController viewDidAppear:NO];
..只是一旦相應的控制器上設置的視圖控制器之前(分配後右)。從此,它在其子視圖控制器上正確調用了這些方法。
我的層次結構是這樣的:
window
UITabBarController (subclass of)
UIViewController (subclass of) // <-- manually calls [navController viewWill/DidAppear
UINavigationController (subclass of)
UIViewController (subclass of) // <-- still receives viewWill/Did..etc all the way down from a tab switch at the top of the chain without needing to use ANY delegate methods
只需調用標籤/導航控制器在第一時間確保所有事件正確轉發上提到的方法。它阻止了我需要從UINavigationBarDelegate
/UITabBarControllerDelegate
方法手動調用它們。
旁註: 奇怪的是,當它沒有工作,私有方法
- (void)transitionFromViewController:(UIViewController*)aFromViewController toViewController:(UIViewController*)aToViewController
..你可以從調用堆棧上看到一個工作的實施,通常調用viewWill/Did..
的方法,但並沒有到我執行了上述(即使它被稱爲)。
我認爲這是非常重要的UITabBarController
是在窗口級別,但文件似乎支持這一點。
希望明確(ish),很樂意回答更多問題。
來源
2011-07-26 17:10:55
Sam
直到我意識到我是錯誤理解用途的UIViewController子類通常我有這個問題。看看這個問題。 http://stackoverflow.com/questions/5691226/am-i-abusing-uiviewcontroller-subclassing/5691708#comment-6507338 – averydev 2011-04-17 23:26:47
請當心!不再適用於iOS 5!調用viewWillAppear和viewDidAppear自動調用 – 2011-11-10 17:23:51