简体   繁体   English

检测目标视图控制器中的导航弹出

[英]Detect navigation pop in destination view controller

Using a UINavigationController in Xamarin.iOS (targetting iOS 11/12) I'd like to detect, in the destination View Controller when I'm being navigated back to - ie a pop. 在Xamarin.iOS中使用UINavigationController (目标是iOS 11/12)我想在我导航 目标视图控制器时检测到 - 即弹出窗口。 I specifically want to exclude/detect when the destination VC is being pushed for the first time. 我特别希望排除/检测第一次推送目标VC的时间。

To be explicit, given that we've navigated from A to C, and back to B: 明确一点,假设我们已从A导航到C,然后返回B:

[A] -(push)-> [B] -(push)-> [C] -(pop)-> [B]

I'd like to detect/discriminate - in B - between the initial push from A -> B and a subsequent pop from C -> B . 我想 A -> B的初始推送和C -> B的后续弹出之间检测/区分 - C -> B

Conceptually this is identical to the questions posed here and here and I should - apparently - be able to use a combination of isMovingToParentViewController / IsBeingPresented / View.Window in ViewWillAppear() but, having tried the approaches in both the linked questions (and a few other things), I'm not seeing the expected values for these properties; 从概念上讲,这与这里这里提出的问题完全相同,我应该 - 显然 - 能够在ViewWillAppear()使用isMovingToParentViewController / IsBeingPresented / View.Window的组合,但是在两个链接的问题中尝试了这些方法(以及一些其他事情),我没有看到这些属性的预期值; they're always False , and View.Window is always null . 它们总是为False ,而View.Window总是为null

Is this a Xamarin.iOS-specific quirk, or am I missing something? 这是一个特定于Xamarin.iOS的怪癖,还是我错过了什么? If it is Xamarin-related is there a workaround? 如果是Xamarin相关的是否有解决方法? And if not does anyone have a bare-bones C# example of this detection working? 如果没有,那么这个检测的C#示例是否有效?

I'd prefer not to have to maintain state in the navigation controller (or delegate, or individual VCs) since the app structure could change in the future. 我不希望在导航控制器(或委托或单个VC)中维护状态,因为应用程序结构将来可能会发生变化。 However, a solution that made use of the nav controller or delegate to indicate the direction of navigation (pop vs push) to the destination VC - 'B', above - would be acceptable. 但是,使用导航控制器或委托来指示导航方向(弹出与推送)到目的地VC - 'B'的解决方案是可以接受的。

If you want to manipulate navigation operation, isMovingToParentViewController should work. 如果要操纵导航操作, isMovingToParentViewController应该可以工作。 Place it in your B controller's view will appear lifecycle event: 将它放在B控制器的视图中会出现生命周期事件:

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    if (IsMovingToParentViewController)
    {
        // Come from A
    }
    else
    {
        // Pop from C
    }
}

Here is the effect, I tested it on iOS 12: 这是效果,我在iOS 12上测试过:

在此输入图像描述

Another way to detect it is: When you want to push B from A, you have to initialize a new B for pushing so B's ViewDidLoad event will be called. 检测它的另一种方法是:当你想从A推B时,你必须初始化一个新的B用于推送,这样就可以调用B的ViewDidLoad事件。 When popping from C, B has existed in your navigation stack so that the ViewDidLoad won't be fired. 从C弹出时,B已存在于导航堆栈中,因此不会触发ViewDidLoad

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM