Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add NavigationBar (without NavigationController) and SearchBar to a TableView

I get the feeling this really shouldn't be giving me as much trouble as it is and that I'm missing something, but I just can't seem to get this to work. So I want a TabBar that switches between 3 different TableViews. All of them need to have a NavigationBar but not a NavigationController (as it's all taking place after an initial modal segue into the TabBarController). the perfect example of what I'm after is the Apple Maps bookmarks.

I've tried three different methods all of which have failed in different ways:

  1. Add a TableView, then add a NavBar to this, then add the SearchBar. The SearchBar simply replaces the NavBar.
  2. Add a View, then add the NavBar to this. Then add the TableView as a subView. When I add the SearchBar to this subView it ends up underneath the prototype cell (didn't try running this as I would have to write a new ViewController to handle the fact that the TableView is now a subview and didn't think it was worth it just yet given the layout problems).
  3. Add a View, then add a NavBar to this. then add a ContainerView which has a TableViewController. This simply crashes on running. I'm not sure what the error is here as I'm not really too sure where to catch any exceptions...

I've implemented most of my interface in the IB so far and would like to keep it that way as much as possible, but I'm sure there must be something obvious I'm missing here. I've not been iOS programming for very long so go easy on me if I'm missing something fundamental! Any help would be appreciated greatly. Even if it's just saying which above approach I should go with and keep trying at. Thanks!

Edit

I should probably mention that when I said TableViewController I meant a custom subclass of a TableViewController which uses a FetchedResultsController to populate the cells.

like image 649
GraemeArthur Avatar asked Aug 24 '13 12:08

GraemeArthur


2 Answers

If you're going to have a modal segue to a new view controller (or a tab bar controller with tabs that point to navigation controllers), and you want that view controller to have a navigation bar, then you should select the view controller you want to present modally, embed it in a navigation controller (Editor->Embed In->Navigation Controller), and then modally segue to the navigation controller. Same concept applies for your tab bar controller except they will be relationship segues. image of modal segue with navigation bar

like image 104
John Erck Avatar answered Oct 10 '22 22:10

John Erck


You should add a navigation bar to a UIViewController (not a table view controller), then add the table view, and to that table view add a search bar and a cell. I've tested this, and it works fine.

like image 27
rdelmar Avatar answered Oct 10 '22 23:10

rdelmar