2

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!!

4 Answers 4

8

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]; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that would work great. Actually, I would like a Home button to take the user back to their home screen with the buttons. Maybe even an image of home.
1

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.

4 Comments

I think I understand, but will this work if I have many buttons on my UIViewComtroller? I will have several buttons each doing something slightly different.
It should work, provided you set up your MVC design properly. Depending on the button that was pressed you would push different view controllers or init a common view controller with different data.
Ok, so to make sure I understand you correctly. I should change my UIViewController to make the UINavController the root view controller. But then how do I make the nav controller show the tableviewcontroller?
First: declare your viewController, but do NOT set it to the rootViewController of your window. Second: create a UINavigationController and use the -initWithRootViewController: method and pass in your viewController. Set that to the root of your window. When a button is tapped, use [self.navigationController pushViewController:animated:] to push your new view controller.
1

Your UITableViewController subclass is the root view controller of the UINavigationController. Therefore, a back button will not be displayed automatically. If tapping on a table row pushes a second View Controller onto the stack, then that second View Controller will display a back button in its Navigation Bar. So, this is the expected behavior.

You can add a "back" button yourself by creating one (you'd need an image that looks like a back button, or draw one in code) and add it as the leftBarButtonItem of the TableViewController's navigationItem, but I wouldn't do that. A View Controller presented modally shouldn't go "back". The button instead should be something like "Close", "Dismiss", or "Cancel".

Comments

0

In my case, I had a rootviewcontoller that didn't have a visible navigation bar but the pushed viewcontollers all needed a navigation bar with a back button but they weren't showing. I'm working in Xcode 6.1. In the viewDidAppear for the rootviewcontroller, I set:

self.title=@"a title"; self.navigationController.navigationBarHidden=YES;

In the viewDidLoad for the pushed view controllers that need the navigation bar to be visible, I set: self.navigationController.navigationBarHidden=NO; self.navigationItem.hidesBackButton=NO;

It all seems a bit obvious but I was going round in circles trying to show the navigation bar with the back button!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.