Search code examples
iosviewcontrollerback-button

set the back button from the pushViewController view


I have a viewController A, is it possible that I pre-set the back button of viewController B when it is pushed to.

Controller *B = [Controller viewControllerFromStoryboardWithProfile: profile editMode: NO];

    UIBarButtonItem *newBackButton =
    [[UIBarButtonItem alloc] initWithTitle:@"Home"
                                     style:UIBarButtonItemStyleBordered
                                    target:nil
                                    action:nil];

    [B.navigationItem setBackBarButtonItem:newBackButton];

    [self.navigationController pushViewController:B animated:YES];

Something like this?


Solution

  • Try this code:

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Home"
                                     style:UIBarButtonItemStyleBordered
                                    target:nil
                                    action:nil];
    
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];
    
    [self.navigationController pushViewController:anotherViewController];
    

    source: How to create backBarButtomItem with custom view for a UINavigationController