2012-02-10 53 views

回答

4

落實UINavigationControllerDelegate方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 

,並對其進行查詢,找出當前顯示視圖控制器:

navigationController.topViewController 

這是你的出發點之一。

+0

我可能很笨,但我不明白。我在我的2級viewcontroller中實現了'UINavigationControllerDelegate'方法。當我NSLog(@「%@」,navigationController.topViewController);''我剛剛得到當前正在顯示的'UIViewController'(2級)(不是我導航的3級視圖控制器或1級視圖控制器)。 – 2012-02-10 19:28:11

+0

@PeterWarbo你不傻,我是。對於那個很抱歉。有沒有簡單的方法,但這是一個應該工作的方法。無論什麼類實例化您的導航控制器,都將其設置爲navigationController.topViewController屬性的KVO(鍵值觀察者)。每當此屬性更改時,它將調用observeValueForKeyPath:並提供上下文參數中的更改字典。從這裏你可以確定舊值是什麼。 – Rayfleck 2012-02-10 20:42:08

3

您可以通過使用viewControllers屬性來查看整個UINavigationController堆棧。

int count = [navigationController.viewControllers count]; 

topController = [navigationController.viewControllers objectAtIndex:count - 1]; 
previousController = [navigationController.viewControllers objectAtIndex:count - 2]; 
//... 
//... 
rootController = [navigationController.viewControllers objectAtIndex: count - count]; 
相關問題