Search code examples
uinavigationcontrollerxamarin.iosuinavigationbarback-button

UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch


How do I pop up an UIAlertView when the back button of a UINavigationBar (controlled by a UINavigationController) was tapped? Under certain conditions, I want to ask the user an "Are you sure?" type of question so he could either abort the action and stay on the current view or pop the navigation stack and go to the parent view.

The most appealing approach I found was to override ShouldPopItem() on UINavigationBar's Delegate.

Now, there is a quite similar question here: iphone navigationController : wait for uialertview response before to quit the current view

There are also a few other questions of similar nature, for example here: Checking if a UIViewController is about to get Popped from a navigation stack? and How to tell when back button is pressed in a UINavigationControllerStack

All of these state "subclass UINavigationController" as possible answers.

Then there is this one that reads like subclassing UINavigationController is generally not a good idea: Monotouch: UINavigationController, override initWithRootViewController

The apple docs also say that UINavigationController is not intended to be subclassed.

A few others state that overriding ShouldPopItem() is not even possible when using a UINavigationController as that does not allow to assign a custom/subclassed UINavigationBarDelegate to the UINavigationBar.

None of my attempts of subclassing worked, my custom Delegate was not accepted.

I also read somewhere that it might be possible to implement ShouldPopItem() within my custom UINavigationController since it assigns itself as Delegate of its UINavigationBar.

Not much of a surprise, this didn't work. How would a subclass of UINavigationController know of the Methods belonging to UINavigationBarDelegate. It was rejected: "no suitable method found to override". Removing the "override" keyword compiled, but the method is ignored completely (as expected). I think, with Obj-C one could implement several Protocols (similar to Interfaces in C# AFAIK) to achieve that. Unfortunately, UINavigationBarDelegate is not an Interface but a Class in MonoTouch, so that seems impossible.

I'm pretty much lost here. How to override ShouldPopItem() on UINavigationBar's Delegate when it is controlled by a UINavigationController? Or is there any other way to pop up an UIAlertView and wait for it's result before possibly popping the navigation stack?


Solution

  • For reference, the route I took after giving up on ShouldPopItem() is to replace the back button with a UIBarButtonItem that has a custom UIButton assigned as it's CustomView. The UIButton is crafted to look like the original back button using two images for the normal and the pressed state. Finally, hiding the original back button is required.

    Way too much code for what it's supposed to do. So yeah, thanks Apple.

    BTW: Another possibility is creating a UIButton with the secret UIButtonType 101 (which is actually the back button) but I avoided this as it may break at any later iOS version.