【问题标题】:Pop UIViewController using Custom Segue?使用自定义 Segue 弹出 UIViewController?
【发布时间】:2016-10-14 16:42:24
【问题描述】:

我正在关注this 在 UINavigationController 中“释放”我以前的视图控制器的回答。

它工作正常,但弹出部分是我难以开始工作的代码。基本上我的应用程序是这样工作的。它从主菜单(视图 1)开始,然后推送到视图 2,我使用自定义推送转场到达视图 3。现在我想使用不同的自定义转场来弹出从视图 3 到视图 2。但是,通过使用下面的代码,它非常快速地弹出到视图 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];
}

我在这里做错了吗?

【问题讨论】:

  • 你试试[navigationController pushViewController:destinationController animated:YES];
  • @NiravDoctorwala 是的,但这并不能解决问题。另一个问题是我专门寻找“流行”动画,而不是“推动”。

标签: ios uinavigationcontroller transition uistoryboardsegue


【解决方案1】:

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

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

【讨论】:

  • 这不是我需要的,因为在我的情况下弹出来自视图 3 -> 视图 1。我正在尝试弹出视图 2
【解决方案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];
    [controllerStack replaceObjectAtIndex:1 withObject:destinationController];
    [controllerStack addObject:sourceViewController];

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

一个更广泛的答案可能是在数组中找到源视图控制器对象的索引,并将目标视图控制器添加到它之前的索引中,将所有内容从先前的索引向前移动一个位置,这样你就不用不要弄乱您的任何其他视图控制器。正如我所说,索引 1 在这种情况下特别适合我。

【讨论】:

    【解决方案3】:

    你想要的是一个“放松”的转场。更多关于这里的信息:https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 2017-06-20
      相关资源
      最近更新 更多