0

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 - i.e. a pop. I specifically want to exclude/detect when the destination VC is being pushed for the first time.

To be explicit, given that we've navigated from A to C, and back to 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.

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; they're always False, and View.Window is always null.

Is this a Xamarin.iOS-specific quirk, or am I missing something? If it is Xamarin-related is there a workaround? And if not does anyone have a bare-bones C# example of this detection working?

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. 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.

Robin Macharg
  • 1,468
  • 14
  • 22
  • Duplicate of https://stackoverflow.com/questions/8228411/detecting-when-the-back-button-is-pressed-on-a-navbar ? Of course that has nothing to do with Xamarin, so there might be some difference, but what it says there is correct. In particular see my answer there. – matt May 22 '19 at 03:42
  • I don't think it's a duplicate, since I'm interested in detecting the change when the destination VC (re)appears, not on the press of the back button when viewing the source. – Robin Macharg May 22 '19 at 10:22
  • Ok, possible duplicate of https://stackoverflow.com/questions/54602662/unified-uiviewcontroller-became-frontmost-detection then. – matt May 22 '19 at 10:57
  • I would be interested in solving this issue in .Net MAUI. So far I have not found an equivalent to IsMovingToParent... in any MAUI methods (like OnAppearing). I want to know when a page has been 'popped to' so I can refresh its lists, etc. only in that situation and not when it is pushed to. – BryanCass Jan 12 '23 at 17:37

1 Answers1

0

If you want to manipulate navigation operation, isMovingToParentViewController should work. Place it in your B controller's view will appear lifecycle event:

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:

enter image description here

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. When popping from C, B has existed in your navigation stack so that the ViewDidLoad won't be fired.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
  • Marked as correct, since it does answer the question, thanks. My Bad: I neglected to mention that my three view controllers are in a tab and that I'm actually interested in the pop to A (not B). I didn't think it made a difference; apparently it does. Discriminating between the initial load and a pop to the root of a navigation controller doesn't work with `IsMovingToParentViewController`. I've got round it with a one-time `firstRun` initially-true boolean that gets changed to false in `ViewDidAppear()`. – Robin Macharg May 22 '19 at 10:19