Search code examples
iosobjective-cuitableviewuisearchcontroller

Calling performSegueWithIdentifier on parent view from child doesn't change view


I have a searchController and it has a searchResultsController. When I search in the search bar the results controller shows the search results, but when I tap on one of the cells it doesn't show the detail view controller. To get it to show, I have to hit the cancel button beside the search bar and when the searchResultsController disappears, the detail view appears. I know the perform segue function is being called on the parent view from the child view. The delegates for the tableview of the searchResultsController are all pointing to the searchController.

Why is the view not changing until I hit the cancel button?

Here's some of the code:

Search controller class:

// parent view controller
- (void) showContact:(Contact*)contact
{
    NSLog(@"Did tap contact id:[%@]",contact.id);
    [self performSegueWithIdentifier:@"contactPressed" sender:contact];
}

- (void)contactsSearchTableDelegate:(ContactsSearchTableDelegate *)delegate
                  didTapContact:(Contact *)contact
{
    [self showContact:contact];
}

Delegate Class:

// delegate, searchController and searchResultsController both point here
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate contactsSearchTableDelegate:self didTapContact:[self contactAtIndexPath:indexPath]];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Solution

  • A search results controller is a presented view controller. Its view covers the screen and replaces the view of the view controller we started with. Thus, that view controller's view is not in the view hierarchy and we can't go anywhere from it. Thus, your code needs to do what you are doing manually now: it needs to start by cancelling the search and thus dismissing the presented view controller, so that we are back to the first view controller and can proceed from there.