set title of back bar button item in ios

若如初见. 提交于 2019-12-03 05:35:25

Try this ,

Objective-C:

UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                            initWithTitle:@"Done"
                            style:UIBarButtonItemStyleBordered
                            target:self
                            action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem=btnBack;

Swift:

var btn = UIBarButtonItem(title: "Done", style: .Plain, target: self, action: "backBtnClicked")

self.navigationController?.navigationBar.topItem?.backBarButtonItem=btn

When you use Storyboards you can change the text of the left bar button item aka the back button with no code. You will obviously need a UINavigationController within your Storyboard so you have the ability to push and pop views.

  1. Select the Navigation item object for the source view controller
  2. Enter text into the Back Button option in the inspector view on the right.

Then when a view is pushed from this view it will use this title set.

Set a backBarButtonItem to the navigationItem of the previous viewController. Setting the topItem only works on the first time viewWillAppear. Please check this post for detail: iOS Set Navigation Bar Back Button Title

NSArray *viewControllerArray = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousIndex = [viewControllerArray indexOfObject:self] - 1;
if (previousIndex >= 0) {
    UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
    previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                     initWithTitle:backButtonTitle
                                                     style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:nil];
}

Try changing the title of the first view controller where you push the second view controller, and set the tittle to the old one when u pop.

Try this:

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                       initWithTitle:@"Back" 
                                       style:UIBarButtonItemStyleBordered
                                       target:self
                                       action:@selector(backButtonClicked:)];
        [self.navigationItem setLeftBarButtonItem: btnBack];


   -(void)backButtonClicked:(UIBarButtonItem*)sender
     {
        //do ur stuff
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!