UINavigationController 在视图上调用 willMoveToWindow 和 didMoveToWindow 两次,在显示动画的情况下 [英] UINavigationController calls willMoveToWindow and didMoveToWindow twice on a view, in the case of show animation

查看:27
本文介绍了UINavigationController 在视图上调用 willMoveToWindow 和 didMoveToWindow 两次,在显示动画的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够检查视图何时开启/关闭屏幕,让我们称他为 myView.

I want to be able to check when a view, let's call him myView, goes on/off screen.

通过检查是否在 myView 上调用了 willMoveToWindow 和 didMoveToWindow,我能够做到这一点.

I was able to do so by checking if willMoveToWindow and didMoveToWindow are called on myView.

我的问题始于带有动画的 UINavaigationController:

My problem started with UINavaigationController with animation:

假设我们有两个屏幕:带有按钮的屏幕 A 将导致带有后退按钮作为导航控件的 Show segue.

Lets say we have a two screens: Screen A with a button that will result in a Show segue with a back button as the navigation control.

问题是:单击按钮后,屏幕 A 的视图会被调用两次:

The problem is: The views of screen A are called twice once clicking on the button:

  • 第一次使用 THE REAL WINDOW 调用 viewWillMoveToWindow.
  • 然后,在下一个循环中,该视图将被调用,并带有一个 nil 窗口.

在这两次调用之间,我得到一个非法状态,我认为视图即将呈现,尽管他很快就会被删除..

In between those two calls, I get an illegal state in which I think the view is about to be presented, although he will be removed soon..

我尝试通过在导航控制器上使用 _transitionAnimationContext 来解决此问题,并查看谁是 fromViewController.如果是我的VC,我会忽略这个电话,因为我即将被删除.这种方法的问题是使用了私有 API,我试图避免这种情况.

I tried to resolve this by using the _transitionAnimationContext on the navigation controller and see who is the fromViewController. If it is my VC, I will ignore the call, since I am about to be removed. The problem with this approach is the use of private APIs, which I am trying to avoid.

有什么想法吗??

推荐答案

所以,我会回答我自己的问题 :)

So, I will answer my own question :)

搜索和搜索后,我遇到了以下 UIViewController 扩展:

After searching and searching, I came across the following UIViewController extension:

@interface UIViewController(UIViewControllerTransitionCoordinator)
@property(nonatomic, readonly, nullable) id <UIViewControllerTransitionCoordinator> transitionCoordinator NS_AVAILABLE_IOS(7_0);

在 UIViewControllerTransitionCoordinator.h 中

in UIViewControllerTransitionCoordinator.h

此属性保存当前的过渡协调器,您可以从中访问 fromViewController,在这种情况下,它将指示视图 willMoveToWindow 用于过渡而不是呈现.

This property holds the current transition coordinator, from which you can reach the fromViewController, which in this case will indicate the view willMoveToWindow for transition and not for presentation.

享受吧!

这篇关于UINavigationController 在视图上调用 willMoveToWindow 和 didMoveToWindow 两次,在显示动画的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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