2016-06-14 90 views
0

我正在關注this答案,以便在UINavigationController中「釋放」我之前的視圖控制器。使用自定義Segue流行UIViewController?

它工作正常,但彈出部分是我難以開始工作的代碼。基本上我的應用程序是這樣工作的。它從一個主菜單(視圖1)開始,然後推到視圖2,並使用自定義push segue來查看視圖3.現在我想使用不同的自定義視圖,現在從彈出視圖3到視圖2。但是,通過使用下面的代碼,它很快地彈出到視圖1,然後最終推到視圖2.它看起來像視圖控制器轉換是不自然的,我只是想實現通常的彈出轉換,而不是通過使用自定義的繼續以「釋放」源視圖控制器。

這是我的代碼,我現在使用無濟於事:

- (void)perform { 
    // Grab Variables for readability 
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController]; 
    UIViewController *destinationController = (UIViewController*)[self destinationViewController]; 
    UINavigationController *navigationController = sourceViewController.navigationController; 

    // Get a changeable copy of the stack 
    NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers]; 
    // Replace the source controller with the destination controller, wherever the source may be 
    [controllerStack addObject:destinationController]; 

    // Assign the updated stack with animation 
    [navigationController setViewControllers:controllerStack animated:YES]; 
} 

有什麼我做錯了嗎?

+0

你試過[navigationController pushViewController:destinationController animated:YES]; –

+0

@NiravDoctorwala是的,但是這並沒有解決問題。另一個問題是我特別尋找「流行」動畫,而不是「推動」。 –

+0

http://stackoverflow.com/questions/10281545/removing-viewcontrollers-from-navigation-stack –

回答

0

如果你只想彈出查看3返回到查看2,你不能只是做這樣的事情嗎?

- (void)perform { 
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController]; 
    UINavigationController *navigationController = sourceViewController.navigationController; 
    [navigationController popViewControllerAnimated:YES]; 
} 
+0

這不是我所需要的,因爲在我的情況下彈出視圖3 - >視圖1.我試圖彈出查看2 –

0

我不知道,如果這個答案是局部的給我,但我的記錄導航堆棧層次,並與陣列玩弄後,我這樣做,它很適合我。

- (void)perform { 
    // Grab Variables for readability 
    UIViewController *sourceViewController = (UIViewController*)[self sourceViewController]; 
    UIViewController *destinationController = (UIViewController*)[self destinationViewController]; 
    UINavigationController *navigationController = sourceViewController.navigationController; 

    // Get a changeable copy of the stack 
    NSMutableArray *controllerStack = [NSMutableArray arrayWithArray:navigationController.viewControllers]; 
    [controllerStack replaceObjectAtIndex:1 withObject:destinationController]; 
    [controllerStack addObject:sourceViewController]; 

    [navigationController setViewControllers:controllerStack animated:NO]; 
    [navigationController popViewControllerAnimated:YES]; 
} 

更廣泛的答案很可能是尋源視圖控制器對象的索引數組中,你的目標視圖控制器之前將其添加到索引中,從先前指數上移的一切向前一個地方,這樣你就不會搞亂你的其他視圖控制器。正如我所說,指數1在這種情況下尤其適用於我。