Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController not showing back button

I have a storyboard with a UIViewController with some buttons. One of the buttons does a modal segue to a UINavigationController and the NavController has a UITableViewController embedded in it. When I click the button on the home screen it advances to the NavController but there is not back button.

So how do I get a back button? I have tried a few things, but no luck.

Thanks for the help!!

like image 804
LilMoke Avatar asked Apr 20 '26 14:04

LilMoke


2 Answers

If you are presenting the navigation controller modally, then the tableview controller is the only view controller your new navigation controller has pushed. There would not be and should not be a back button in that case.

You'd be better off adding a cancel/done button to the nav bar via the tableview controller, which dismisses the modal view.

In your tableView controller viewDidLoad: method:

UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped:)];
self.navigationItem.leftBarButtonItem = done;

//Release done if not using ARC

Then add (the simplest implementation of) a dismiss method:

- (void)doneButtonTapped:(id)sender {

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

}
like image 199
ChrisH Avatar answered Apr 22 '26 03:04

ChrisH


A UINavigationViewController only manages a stack of view controllers. You push a viewController onto the stack, and when you hit "back" you are popping a view off of the stack.

Since you are presenting the UINavigationController modally, it has no knowledge of what was presented before it. The correct way to get the desired behavior is to set your main UIViewController to the UINavigationController's root view controller. When the user taps a button, you push the new UITableViewController onto the UINavigationController's stack.

like image 36
Matias Avatar answered Apr 22 '26 04:04

Matias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!