简体   繁体   中英

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?

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
NavigationItem.SetHidesBackButton(true,true);

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM