简体   繁体   中英

iOS Back button title is not changing

I have an issue with "back" button in UINavigationController

I have a root view controller with method implemented like this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"NEW BACK" style:UIBarButtonItemStylePlain target:nil action:nil];
}

Despite of this code, my back button title is still a title of my root view controller instead of "NEW BACK"

Does anyone sees where is the problem?

Hide your default back button then add a button programatically. Add the code in your viewDidLoad.

self.navigationItem.hidesBackButton = YES;

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [backButton setBackgroundImage:[UIImage imageNamed:@"arrowleft.png"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0.0f, 0.0f, 25.0f, 20.0f);
        UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

        self.navigationItem.leftBarButtonItem = backButtonItem;

Add

btnBackTapped

method anywhere in same class

-(void)btnBackTapped:(id)sender 
{
   [self.navigationController popViewControllerAnimated:YES];
}

Instead of doing this

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"NEW BACK" style:UIBarButtonItemStylePlain target:nil action:nil];

in your RootViewController , do this in the child view controller means the controller which you are pushing.

If you set navigation controller's back button, then it will be applied to all the further viewController, though viewController has its own back button configuration. Try to reset the back button in your navigationController. Hope your code will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM