开发者

How to change uinavigationbar back button text

I am currently trying to change the back button text of a subview that is loaded of a tablecell touch. However even with the way I am trying to implement it it still shows the paren开发者_C百科ts title in the back button.

I am trying to load a new value into the back button inside viewdidload method like so

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];

however its not working.


You need to change self.navigationItem.backBarButtonItem from previous view, not current view (I know, it seems to be a little bit illogical). For example, in your table view you can do the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:@"My title"];
    UIBarButtonItem *boton = [[UIBarButtonItem alloc] initWithTitle:@"Custom back button text" style:UIBarButtonItemStyleBordered target:self action:@selector(mySelector:)];
    self.navigationItem.backBarButtonItem  = boton;
    [boton release];
}


This is where the documentation is not so clear until you have read and re-read and play around with each settings.
To change the title of the default back button add this line in viewDidLoad()

self.navigationController.navigationBar.topItem.title = @"Your Label";

If you want the button to be invisible - then set the value to @"" empty string.


Okay figured it out, I posted this code in the parent views tableView:didSelectRowAtIndexPath: method. this is how my one looks with multiple tablecells the user can select. Hope this helps.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    if (indexPath.section == 0) { //--- picks (first) section

     ChildViewController *childViewController = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:childViewController animated:YES];
    //--- this sets the back button to "Back" every time you load the child view.
     self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];

        if(indexPath.row == 0) { //--- picks row
            childViewController.title = @"Bike";
        }
        if(indexPath.row == 1) {
            childViewController.title = @"Model";
        }
        [vehicleSearchResponseTableViewController release];
    }


}


The best way with Xcode 5 to change the back button name is to edit the Back Button field in IB of the Navigation Item on the View Controller to which the back button will return. For example, if you have a list TableViewController that goes to a detail screen, change the Back Button on the list TableViewController's Navigation item (in IB) to change the back button name that the detail screen displays.


It's not gonna work the way you're trying to do. Navigation button will always have title of previous view. What you can do though - change title of first view before pushing the new one. This is the only was I could find to solve same problem.


Your code looks fine so far.

Is this code executed before the

[super viewDidLoad];

statement (wrong) or after it (good)?


After viewDidLoad

self.navigationController!.navigationBar.backItem?.title = "Back"


My suggestion was to add a separate Label to parents Page title Bar. Worked fine for me.

let titleLabel = UILabel(frame: CGRect(x: (view.frame.width * (3/8)), y: 0, width: (view.frame.width * 0.25 ), height:30))
        titleLabel.text = "Title"
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: 20)
        navigationItem.titleView = titleLabel 


This article should do what you want.

From the author:

As you can see above, all you need to do is set the leftBarButtonItem of the controller and it will hide the back button. The selector handleBack now handles the back event and you need to then manually pop the view controller off the UINavigationController’s stack. You can implement any of your own code in there, even blocking leaving the page.


A good way to do this is to set the title of the back button in the parent view controller before calling the child like this:

[self.navigationItem.backBarButtonItem setTitle:@"Your Custom Title"];

This title will be placed in the back button on the child view. The important point to get here is this title is used in the child view as the back button to the parent view.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜