Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationItem.BackButtonItem is null...?

I've pushed my View on to the NavigationController stack, I'm in the ViewLoaded, and I find that NavigationItem.BackBarButtonItem is null. Why is that? How can I either disable it (temporarily, and I'd prefer not to hide it in this very particular case) and how could I consider renaming what it shows?

like image 735
Driss Zouak Avatar asked Dec 04 '22 22:12

Driss Zouak


2 Answers

As @Andrew said you can use

self.navigationItem.HidesBackButton = false;

to hide the Back Button and also you can use

NavigationItem SetHidesBackButton (false, true);

if you want to hide it with animation. But I want to tell you the part about

NavigationItem.backBarButtonItem is null.

Well the back button item that you see on the navigation bar belongs to previous view controller so thats why you are getting null.

And if you create a bar button item with some weird name and extra features(I dont know what else you can do with it.) and add it to your NavigationItem.backBarButtonItem it will be shown in the next view controller which is gonna be pushed on top of it.

That is the reason for your NavigationItem.backBarButtonItem is null part of your question.

like image 65
Robin Avatar answered Dec 14 '22 03:12

Robin


Apple recommends you set the Back button from the view controller you instantiate the new view from, and not in the viewDidLoad method.

This is how to do it:

SecondViewController secondView = new SecondViewController ("secondview");
UIBarButtonItem backButton = new UIBarButtonItem ("Title", UIBarButtonItemStyle.Plain);  // style, target and action will be overridden, regardless of value
NavigationItem.BackBarButtonItem = backButton;
NavigationController.PushViewController (secondView, true);

Note that when creating a backBarButtonItem, only the title can be set, the other values will be overridden.

To explicitly disable the Back button:
this.navigationItem.BackBarButtonItem = nil

like image 33
antalkerekes Avatar answered Dec 14 '22 04:12

antalkerekes