2012-03-25 71 views
0

我的NavigationController中遇到了一些有問題的行爲。我有一些UINavigationControllers設置在我的TabBarController(4個選項卡是特定的)。我的AppDelegate符合UITabBarControllerDelegate,當切換標籤頁時,我使用它觸發popToRootViewController,所以當用戶來到該標籤時,它返回到rootViewController。除此之外,所有的工作都可以正常工作:除了「返回」該Tab之外,在加載rootView的生命週期之前,加載的最後一個viewController的viewWillAppear被調用。 我的委託實現如下所示:UITabBarController中的UINavigationController視圖生命週期問題

#pragma -mark TabBarController 
///////////////Pop our navigationControllers to the rootView when Tab is changed//////////////////////////////// 

- (void) tabBarController: (UITabBarController *) tabBarController didSelectViewController: (UIViewController *) viewController { 

    if ([viewController isKindOfClass:[UINavigationController class]]) { 
     NSLog(@"******POP TO ROOT VIEW*******"); 
     [(UINavigationController*)viewController popToRootViewControllerAnimated:NO]; 
    } 

} 

現在我意識到委託方法是didSelectViewController,但沒有任何辦法阻止這種行爲。一些可能會沿着did * Deselect * ViewController的行將會很好,但這不是由API提供的。我真的不會有錯誤的觀點,因爲我在那裏啓動了一個多線程的過程。有什麼建議麼?

回答

1

你可以有一個取消的通過實施shouldSelectViewController等價的:和回答YES:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 

    UIViewController *currentVC = tabBarController.selectedViewController; 
    // whatever you would like to do on deselect, like 
    [currentVC popToRootViewControllerAnimated:NO]; 
    // it will be at the root when you get back to it, and as a bonus, you have 
    // a handle to viewController, which is about to be selected 

    return YES; 
} 
+0

改變從下來的UINavigationController的堆棧選項卡,當我有一個致命的錯誤旋轉。這解決了它。最後! – Suz 2015-02-12 22:51:39