简体   繁体   中英

UISearchDisplayController is not dismissing [ios8, swift]

I wish to dismiss the UISearchDisplayController when the user presses on one of the scope options. To achieve this I have implemented this code, which doesn't appear to work:

func searchBar(searchBar: UISearchBar!, selectedScopeButtonIndexDidChange selectedScope: Int) {
    // Hide the search display
    self.searchDisplayController.setActive(false, animated: true)
}

The UISearchDisplayController appears do as expected but immediately, and over the top, loads another UISearchDisplayController. Help!

Try using NO instead of false in your setActive method.

See UISearchDisplayController docs.

Your code looks correct, you may need to unwrap the searchDisplayController

Change this line:

self.searchDisplayController.setActive(false, animated: true)

to this:

self.searchDisplayController!.setActive(false, animated: true)

But if you're not getting a compiler error, then that should not be your issue. In that case, the problem is likely to be somewhere else in your code.

I would suggest doing a quick check to verify your class is setup as the UISearchBar and UISearchDisplay delegate by checking your class declaration.

class myClass: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate { ... }

If you're class is not setup as the delegate it will not receive the proper messages.

You should be able to verify the event by outputting something to the log.

Add this line to the method to verify:

println("scope button pressed")

If you're not seeing the event logged then the method is not being called. You can turn your focus to your UI.

The SearchDisplayController requires a delegate outlet and a referencing outlet to your class file in order to work properly.

I hope this helps.

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