2011-11-18 62 views
3

我RootViewController的之後,有一個工作流程,會是這樣的popToViewController,然後pushViewController

viewController1 -> push viewController2 -> push viewController3 -> ***** viewController2 

我想popToViewController3。然而,我需要先去viewController1,因爲viewController1基本上決定了哪個viewController2實例推送。所以我

// pseudo code 
[popToViewController:vc1 animated:NO]; 
[self.navigationController pushViewController2 animated:YES]; 

我推,所以我不最終得到VC2,VC3,VC2,VC3,VC2等之前,但在做這個基本上是流行,因爲它以動畫的最後的viewController的推動,它給用戶覺得一個新的viewController被壓入堆棧,vs彈出到前一個viewController。有沒有辦法解決這個問題?如果我彈出流行音樂,而不是最後一個推動,我沒有得到正確的viewController我想在它的外觀上。謝謝。

回答

0

您可以檢索(viewControllers),修改和「重新安裝」(setViewControllers)由導航控制器管理的視圖控制器列表,並且可以(理論上)「重新安裝」動畫。 (不過,我在實踐中遇到了動畫方面的一些問題,儘管我不記得這些細節。)

1

,你可以使用類似

[self.navigationController setViewControllers:[NSArray arrayWithObject:yourViewController] 
            animated:YES]; 

您可以填寫你想要的堆棧上的視圖控制器的陣列。

1
@interface UINavigationController (PushThenPop) 
-(void)popToViewController:(UIViewController*)rootViewController 
    thenPushViewController:(UIViewController*)pushViewController; 
@end 


@implementation UINavigationController (PushThenPop) 
-(void)popToViewController:(UIViewController*)rootViewController 
    thenPushViewController:(UIViewController*)pushViewController 
{ 
    NSMutableArray *viewControllers = self.viewControllers.mutableCopy; 

    // Pop viewcontrollers until we reach the rootViewController 
    while(viewControllers.count >= 1) { 
     id last = [viewControllers lastObject]; 
     if (last == rootViewController) break; 
     [viewControllers removeLastObject]; 
    } 

    // Bail out the array is empty. We expect the array to contain at least 1 element, the rootViewController. 
    if (!viewControllers.count) { 
     return; 
    } 

    // Push the new view controller 
    [viewControllers addObject:pushViewController]; 
    self.viewControllers = viewControllers.copy; 
} 
@end 
相關問題