2016-06-14 86 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在这种情况下尤其适用于我。