Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController: Hiding Back Button on One View Hides it For All Views

I have a UINavigationController that contains 3 UIViewControllers on the stack.

View A - is the root
View B - is pushed by View A and has `self.navigationItem.hidesBackButton = YES;`
View C - is pushed by View B and has `self.navigationItem.hidesBackButton = NO;`

View C does not show the back button, even though I have hidesBackButton set to NO. How can I resolve this?

like image 303
Sheehan Alam Avatar asked Feb 21 '11 04:02

Sheehan Alam


2 Answers

Update
A possible bug in 4.2 as it works till 4.1 sdks

I have tried this and mine is working perfectly. I am just posting the implementation of B view controller (BVC) and C view controller (CVC). My initial guess is that you are not setting the title of BVC in viewDidLoad.

@implementation BVC


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"I am B";
}


- (void) viewWillAppear:(BOOL)animated{
    self.navigationItem.hidesBackButton = YES;
}

- (IBAction)pushB:(UIButton *)sender{
    CVC *cvc = [[CVC alloc] initWithNibName:@"CVC" bundle:nil];
    [self.navigationController pushViewController:cvc animated:YES];
    [cvc release];
}
@end

@implementation CVC

- (void) viewWillAppear:(BOOL)animated{
    self.navigationItem.hidesBackButton = NO;
}
@end
like image 112
Madhup Singh Yadav Avatar answered Nov 15 '22 20:11

Madhup Singh Yadav


I think you have to set that property before you push or pop a view controller to affect the next view controller, setting it for the current viewcontroller in viewWillAppear is too late.

Edit: this looks like a bug in 4.2! The back button remains hidden both in the 4.2 simulator and on the device with 4.2, but it works in the 3.2, 4.1, and 4.0 simulators!

Here's the code where when pushing a VC with a hidden back button:

- (IBAction) goto2nd
{
    SecondVC *vc = [[[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil] autorelease];
    vc.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

That is all that should be needed, each VC has its own navigationItem, it's not a global setting, so you don't need to bother undoing it to restore the back button (at least when popping back to a VC where it is set to "NO").

like image 35
Bogatyr Avatar answered Nov 15 '22 20:11

Bogatyr