views:

1559

answers:

1

I have a way of hiding the back button used by the navigation controller. It's set by the previous controller, not the one managing the current view, and that makes it tricky to get to. I needed to do this in editing mode so that I could prevent the user from navigating away from the screen.

if(self.editing) {
    // Get rid of the back button 
    UIView *emptyView = [[UIView alloc] init];;
 UIBarButtonItem *emptyButton = [[[UIBarButtonItem alloc] initWithCustomView:emptyView] autorelease];
 [self.navigationItem setLeftBarButtonItem:emptyButton animated:YES];
} else {
    // Restore the back button
 [self.navigationItem setLeftBarButtonItem:nil animated:YES];  
}

Is there a better way to do this?

+8  A: 

use this to hide back button

[self.navigationItem setHidesBackButton:YES]

use this to show back button

[self.navigationItem setHidesBackButton:NO]
Raj
That works, thanks. There is one small difference--no animation. I solved that with these versions: [self.navigationItem setHidesBackButton:YES animated:YES]; [self.navigationItem setHidesBackButton:NO animated:YES];
Steve Weller
Be sure to put this code into the ViewController where you want the button hidden -- not the previous controller (which the original question required).
jm