简体   繁体   中英

UISearchController not dismissed when View is pushed

I know this is a long write-up, but it's just one issue I promise.

The Setup

I'm getting some very strange behavior with a UISearchController. Let me describe the hierarchy, then I'll explain step by step what happens in my video:

The first view you see is a regular ViewController, with a tableView and the UISearchController as completely separate entities. The UISearchController has its own searchResultsController that I set when I create it:

let searchResultsController = MyResultsControllerClass()
searchController = UISearchController(searchResultsController: searchResultsController)

That's the basic setup.

The Behavior

A link to a video of the behavior

The MyResultsControllerClass has a tableView, as well as a pointer back to the master view controller all these views are housed in. When the searchController's searchBar begins searching, you see that tableView show up. Then I tap a result, Programming Club. The MyResultsControllerClass uses the pointer to the master view to push a new view controller (just details about that event) like so:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let activityDetail = ActivityDetailViewController(nibName: nil, bundle: nil, activity: searchResults[indexPath.row])
    parentController?.navigationController?.pushViewController(activityDetail, animated: true)
}

You can see this happen behind the searchBar, but the searchController is not "covered up" by the presented view! This is my issue. As you can see, I can still interact with it. When I press "Cancel", it finally disappears.

What I've tried

Now, I thought this might have to do with the fact that a UISearchController is in fact a UIViewController, if I understand correctly. So I attempted pushing from the UISearchController, nothing happens. I tried embedding the UISearchController in its own UINavigationController and pushing from searchController.navigationController, nothing happens. I can completely cancel the editing before pushing, but I want the editing to still exist when they navigate back.

What am I doing wrong? Is there a better way to do this?

I have the same exact issue, I have solved it by setting:

self.definesPresentationContext = YES;

on the view controller that presents the UISearchController. This is recommended by Apple in WWDC session 228, read about it here: http://asciiwwdc.com/2014/sessions/228 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM