Forum Xamarin.iOS

Alert before pop

john82john82 ITMember ✭✭✭


I need to show an alert before popping a UIViewController from the UINavigationController. How this can be done with Xamarin?
Some one posted a solution in Obj-C.

This can be done by subclassing UINavigationController and overriding
(BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item



  • john82john82 ITMember ✭✭✭

    Ok, I've found the solution by myself and thanks to stackoverflow :)
    Reading this

    It seems that it is possible to handle the shouldPopItem method of the NavigationBar in this way:

    1. Subclass a UINavigationController
    2. Mark your custom UINavigationController with the IUINavigationBarDelegate
    3. Add this method with the Export attribute

      [Export ("navigationBar:shouldPopItem:")]
      public bool ShouldPopItem (UINavigationBar navigationBar, UINavigationItem item)

    Now you can handle popping in the ShoulPopItem method. An example to this is to create an interface like this

    public interface INavigationBackButton
        // This method should return TRUE to cancel the "back operation" or "FALSE" to allow normal back
        bool BackButtonPressed();

    Then mark your UIViewController which needs to handle the back button with this interface. Implement something like this

    public bool BackButtonPressed()
        bool needToCancel = // Put your logic here. Remember to return true to CANCEL the back operation (like in Android)
        return needToCancel;

    Then in your ShouldPopItem Implementation have something like this
    tanks to:

            public bool ShouldPopItem(UINavigationBar navigationBar, UINavigationItem item)
                if (this.ViewControllers.Length < this.NavigationBar.Items.Length)
                    return true;
                bool shouldPop = true;
                UIViewController controller = this.TopViewController;
                if (controller is INavigationBackButton)
                    shouldPop = !((INavigationBackButton)controller).BackButtonPressed();
                if (shouldPop)
                        () =>
                    // Workaround for iOS7.1. Thanks to @boliva -
                    foreach (UIView subview in this.NavigationBar.Subviews)
                        if(subview.Alpha < 1f)
                            UIView.Animate(.25f, () => subview.Alpha = 1);
                return false;                          
  • batilakbatilak Member ✭✭

    Hi do you have any git pointing to the same?

Sign In or Register to comment.