开发者

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 disa开发者_C百科ble it (temporarily, and I'd prefer not to hide it in this very particular case) and how could I consider renaming what it shows?


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.


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


to hide the back button, use this:

self.navigationItem.hidesBackButton = NO;

and then to show it again, use:

self.navigationItem.hidesBackButton = YES;

you can set this hidden or shown at any point in your code; it doesn’t nexessarly have to be in viewDidLoad

to rename what the back button shows:

 UIBarButtonItem *clearAll = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashMethod)];
 self.navigationItem.leftBarButtonItem = clearAll;

and just like the back button, you can set this in any method, not just viewDidLoad

let me know if you need any more help

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜