Add a left bar button item to UINavigationController when no back button is present

ios, iphone, objective-c, uinavigationcontroller, uiviewcontroller

Solution

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if(navigationController.viewControllers.count != 1) { // not the root controller - show back button instead
        return;
    }
    UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
                                                                              target:self
                                                                              action:@selector(menuItemSelected:)];   
    [viewController.navigationItem setLeftBarButtonItem:menuItem]; 
}

Problem

I'd like to add a default left bar button item to my navigation bar. It should only display when there is no back button supplied by the UINavigationController. What is the best approach?

Original source