Pop UIViewController using Custom Segue?



我按照这个答案在UINavigationController中"释放"我以前的视图控制器。

它工作正常,但是弹出部分是我难以开始工作的代码。基本上我的应用程序是这样工作的。它从主菜单(视图 1)开始,然后推送到视图 2,我使用自定义推送 segue 进入视图 3。现在我想使用不同的自定义 segue 来弹出,从视图 3 转到视图 2。但是,通过使用下面的代码,它会非常快速地弹出到 View 1,然后最终推送到视图 2。看起来视图控制器过渡是不自然的,我只是想通过使用自定义 segue 来"释放"源视图控制器来实现通常的流行过渡。

这是我现在使用的代码无济于事:

- (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];
}

我在这里做错了什么吗?

你想要的是一个"放松"的续集。 更多关于这些在这里的信息: https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

如果你只是想弹出视图 3 回到视图 2,你不能做这样的事情吗?

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

我不确定这个答案是否本地化给我,但在记录我的导航堆栈层次结构并玩弄数组后,我这样做了,它对我来说效果很好。

- (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 特别适合我。

最新更新