Forum Xamarin.iOS

Alert before pop

john82john82 ITMember ✭✭✭

Hi,

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.
http://stackoverflow.com/questions/6930229/how-to-show-alert-before-uiviewcontroller-is-popped-from-uinavigationcontroller

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

thanks

Posts

  • john82john82 ITMember ✭✭✭

    Ok, I've found the solution by myself and thanks to stackoverflow :)
    Reading this
    http://stackoverflow.com/questions/6413595/uinavigationcontroller-and-uinavigationbardelegate-shouldpopitem-with-monotouc

    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: https://github.com/onegray/UIViewController-BackButtonHandler/blob/master/UIViewController+BackButtonHandler.m

            [Export("navigationBar:shouldPopItem:")]
            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)
                {
                    //MonoTouch.CoreFoundation.DispatchQueue.DispatchAsync
                    CoreFoundation.DispatchQueue.MainQueue.DispatchAsync(
                        () =>
                        {
                            PopViewController(true);
                        });
                }
                else
                {
                    // Workaround for iOS7.1. Thanks to @boliva - http://stackoverflow.com/posts/comments/34452906
                    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.