How to change text on a back button

前端 未结 20 1971
无人及你
无人及你 2020-11-29 17:24

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this

相关标签:
20条回答
  • 2020-11-29 17:43

    In your parent view controller, set the back button when view loads:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // Do any additional setup after loading the view.
        self.navigationItem.backBarButtonItem = 
           [[UIBarButtonItem alloc] initWithTitle:@"title" 
                                            style:UIBarButtonItemStylePlain 
                                           target:nil 
                                           action:nil];
    
    }
    

    Notice that we don't need to include autorelease at the end with the latest iOS version.

    Hope this helps!

    0 讨论(0)
  • 2020-11-29 17:47

    This work better for me. Try :

     self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
    initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    0 讨论(0)
  • 2020-11-29 17:48

    The back button pulls its text from the title of the parent view controller.

    In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.

    For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."

    in the viewDidLoad method of RootViewController.m:

    self.title = @"Home";
    

    in the viewDidLoad method of SecondViewController.m:

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];    
    

    If you want your back button to read, "Back," set the title of the parent view controller to @"Back";

    0 讨论(0)
  • 2020-11-29 17:50

    And in MonoTouch the following works (in ViewDidLoad of the parent controller):

    NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
    
    0 讨论(0)
  • 2020-11-29 17:50

    If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".

    0 讨论(0)
  • 2020-11-29 17:52
    self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    0 讨论(0)
提交回复
热议问题