0
votes

I have an app with a UITabBarController and a few tabs, each of which contains a UINavigationController. I also have a UISearchController triggered by a search button on the nav bar, which has a custom search results view controller. Search works just fine, as does tapping cancel. I implemented presentSearchController: to present the search controller as a view controller using

[self presentViewController:animated:completion:];

, which also works fine.

Where I run into trouble is if the user dismisses the keyboard (search controller active), and then switches tabs on the tab bar, comes back to the original tab with search controller active, and then taps "Cancel", the underlying UINavigationController's view stack is gone from the main window hierarchy and doesn't get reloaded until I switch tabs and come back with search mode inactive.

This is similar to this issue: "From View Controller" disappears using UIViewControllerContextTransitioning

Except I don't use any custom transitions, and the tab bar still shows after tapping cancel. Printing the view stack shows the tab bar is the only subview of the window until I switch tabs again and everything displays as normal.

What is the best way to go about solving this? I really hate to brute force re-add the navigation controller's view to the window on didDismissSearchController: as suggested. Not only does this seem like a bad idea, but I also run into issues with the z-ordering of the tab bar and the navigation controller when explicitly re-adding the nav controller to the key window.

1
Seems to me to be a problem with the data source for the original view (the view that is beneath the search view in the stack). do you maintain a strong reference to the original view data source, and if you do maybe you need to think about reloading the view data when user taps the cancel button?andrewbuilder
I thought that might be the case as well, but unfortunately the entire navigation controller is missing from the window, so the data reload would have no place to draw to. View tree post cancel only contains the keyWindow and the tab bar -- no nav controller, view controller, or collection view.Chase Holland

1 Answers

0
votes

Adding my brute-force solution:

- (void)didDismissSearchController:(UISearchController*)searchController
{
    if (![self.view isDescendantOfView:[UIApplication sharedApplication].keyWindow]) {
        NSUInteger currIndex = self.tabBarController.selectedIndex;
        NSUInteger tempIndex = self.tabBarController.selectedIndex == 0 ? 1 : 0;

        [self.tabBarController setSelectedIndex:tempIndex];
        [self.tabBarController setSelectedIndex:currIndex];
    }
}