用UINavigationController弹出的自定义segue通过“back”按钮 [英] unwind with custom segue on UINavigationController pop via "back" button

查看:165
本文介绍了用UINavigationController弹出的自定义segue通过“back”按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的storyboard设置:UIViewController与UINavigationController。在表单元格上单击自定义segue将新的UIViewController推送到导航堆栈。都好。但是,然后按导航栏中的返回按钮,它只使用默认的流行动画。

Simple storyboard setup: UIViewController with UINavigationController. On a table cell click a custom segue pushes a new UIViewController onto the navigation stack. All good. But then pressing the "back" button in the navigation bar, it only uses the default pop animation.

如何告诉导航控制器在弹出时使用我的自定义segue背部?我添加了

How do I tell the navigation controller to use my custom segue when popping back? I've added

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier

- (BOOL)canPerformUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender

这两个视图控制器,但他们根本没有被调用。

to both view controllers but they don't get called at all.

我缺少什么? p>

What am I missing?

推荐答案

您可以通过创建自定义转换而不是使用自定义segue来实现此目的。我花了一段时间来理解,但它基本上很简单。只需确保您的第一个视图控制器采用UINavigationControllerDelegate协议。然后,仍然在第一个视图控制器中,实现以下方法:

You can accomplish this by creating a custom transition instead of using a custom segue. It took me a while to understand, but it is basically very simple. Just make sure that your first view controller adopts the UINavigationControllerDelegate protocol. Then, still in the first view controller, implement the following method:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                              animationControllerForOperation:(UINavigationControllerOperation)operation
                                           fromViewController:(UIViewController *)fromVC
                                             toViewController:(UIViewController *)toVC
{
return X // where X is an animation Controller object, an instance of the animator class.  
}

仍然在第一个视图控制器中,例如在ViewDidLoad中,告诉导航控制器第一个视图控制器将作为其委托:

Still in the first view controller, for example in ViewDidLoad, tell the navigationcontroller that the first view controller will act as its delegate:

self.navigationController.delegate = self;

最后但并非最不重要的,创建一个采用UIViewControllerAnimatedTransitioning协议的animator类。实现以下方法:

Last but not least, create an animator class that adopts the UIViewControllerAnimatedTransitioning protocol. Implement the following methods:

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

第一种方法保存转换代码。轻触后退按钮时,将自动使用此转换代码。

The first method holds the transition code. This transition code will automatically be used when tapped on the back button.

以下是一个非常简单的示例项目

Here is a very simple example project.

这篇关于用UINavigationController弹出的自定义segue通过“back”按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆