Kye Kye - 1 year ago 72
iOS Question

Add a custom back button to Navigation Bar using MonoTouch

Just want to confirm this, as I'm trying to learn monoTouch alone..

I have a view which I navigate to using NavigationController.PushViewToController(). On the destination view, I have a Navigation Bar. I can add a button to the bar and use code to push to another view (I happen to know where Back is), fine.

Is there a existing "back button" control? Or a way in to code to change the existing back button to say "Go back"?

In Interface Builder I can see there is a property on the navigationItem called "Back". When I add text to this I can see a new BarButtonItem added to the navigationBar. However I never see this button when I navigate to the view in the simulator. If I try to drag the item onto the view manually, the "Back" text is cleared and the button is treated like a custom button.

Do I always have to manually code the back button?

Answer Source

The default back button (the one that takes the name of the previous controller) cannot be customized. But you can hide it and replace that button with a new one.

If you have a controller, you can do that on the viewDidLoad method. Overriding this method you are sure that all the elements have been set.

// allows you to hide the back button

// allows you to create a new customized button
NavigationItem.LeftBarButtonItem = new UIBarButtonItem(...);

UIBarButtonItem takes an handler that you can use to control the navigation.

In the handler you can do this:

NavigationController. PopViewControllerAnimated(true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download