0

I want to filter a list of entries via UISearchBar and show the details after user clicks a result row. Full list view and details view are linked via a navigation controller. The normal use case (without search) works like charm:

 (ListOfAllEntries)
 => (direct click on row)
 ==> (Details view for row)

This is what should also work:

(ListOfAllEntries) 
=> (Search)                  - OK!
==> (ListOfFilteredEntries)  - OK!
===> (click on result row)          - OK!
====> (Details view for row) - BOOUUMMM! UI and Nav.Ctrl broken

I am using a UISearchBar (with UISearchDisplayController) to filter an underlying UITableView. As Apple recommends the filtered search results are displayed in the default second table view (searchDisplayController.searchResultsTableView) on top of my original table view with all entries.

Everything works fine - the entries are filtered, and I get the right result row indexpath. Until the user clicks a search result row and I want to push the details view for the selected row on top of the navigation controller. My target details view displays BUT then my program is broken in the following ways:

  1. the target view displays slided below the navigation bar (see image#2)
  2. If I press "BACK" on the navigation bar I get an empty screen (see image#3) and after a further BACK click my app crashes) uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

I tried displaying the target view (scene) with a segue:

if (tableView == self.searchController.searchResultsTableView) {
    [self performSegueWithIdentifier: @"showFoods" sender: self];
}

and I tried to display the target view via direct push:

FoodViewController *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FoodViewController"];
[self.navigationController pushViewController:fvc animated:YES];

Both approaches result in the same wrong app behavior.

See my images:

  • Why is my details list below the navigation bar?
  • Why is the navigation stack garbled after I push my details scene?

Any hint would be appreciated.

UISearchbar in action:

UISearchbar in action

After click on search result row - my details scene slides below the navigation bar

After click on search result row - my details scene slides below the navigation bar

After pressing "BACK" the scene with all entries does not display

After pressing "BACK" the scene with all entries does not display

My storyboard. Note: The red arrow marks the problem. (The segue in the storyboard works well. But If I want to grammatically go the way of the red arrow, my UI is messed up!).

Red arrow way messes up the UI

DerWOK
  • 1,061
  • 9
  • 13
  • Either of those code methods should have worked. It seems like something is messed up in your controller hierarchy, but it would be hard to diagnose without actually seeing the app. – rdelmar Dec 28 '13 at 00:56
  • OK. Thanks for your comment. So you suspect my navigation controller is messed up BEFORE i push the new view? Can you give me any hint on how to "debug" a navigation controller? Can I dump the stack before and after? How? And: what do you mean by "actually seeing the app"? I added a storyboard screen shot above. And there are 3 more screenshots. Does that help? What else could I provide? – DerWOK Dec 28 '13 at 14:02
  • I don't see anything wrong in your storyboard. What I meant by seeing it, is to actually test it myself, if it's possible to post it somewhere. If the problem is somewhere in IB, it's hard to find the problem without the ability to test the app, and look at all the settings, constraints, etc. in IB. – rdelmar Dec 28 '13 at 17:11
  • Hi rdelmar, thanks for your comment. I was close before posting you my code. But meanwhile I found this SO posting, which put me on the right track. http://stackoverflow.com/questions/19895818 => I will posted an answer to my question below. Thanks for your time. – DerWOK Dec 29 '13 at 17:07

1 Answers1

0

I found the solution.

The error is very unspecific. And thus other people with the same problem are hard to find. Finally I found one here: Navigating Backwards through a UINavigationController Error

Thanks to the above SO question I was put on the right track: I had to delete the content of my tableView: didSelectRowAtIndexPath: method.

Why that? As UISearchBar was a new topic for me, I read some tutorials and some SO questions. And all of them had put code inside tableView: didSelectRowAtIndexPath:. This code was intended to handle the click on a row on the search result table. See here: http://www.appcoda.com/how-to-add-search-bar-uitableview/ and here: Navigating Backwards through a UINavigationController Error

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"showRecipeDetail" sender: self];        
    }
}

For some reason, (maybe new iOS7 behaviour?!), this code is not necessary anymore. Even worse: this code produces the above problems, as iOS7 automaticall triggers the according segue on the search result table and my manuall trigger made a second segue to fire off. Two segues with the same source and target had caused the problems.

So I completely deleted my tableView: didSelectRowAtIndexPath: overwrite method and everything works like a charm!

Community
  • 1
  • 1
DerWOK
  • 1,061
  • 9
  • 13
  • I don't think this is iOS 7 specific behavior. It's not clear what code you're using now. Did you implement shouldPerformSegueWithIdentifier: like the post you linked to? – rdelmar Dec 29 '13 at 17:52