问题描述
当视图控制器未嵌入 UINavigationController 时,我不明白如何使用 UISearchController.
I don't understand how to use UISearchController when the view controller isn't embedded in a UINavigationController.
要说明我的意思,请尝试下载此代码:https://developer.apple.com/documentation/uikit/view_controllers/displaying_searchable_content_by_using_a_search_controller
To show what I mean, try downloading this code: https://developer.apple.com/documentation/uikit/view_controllers/displaying_searchable_content_by_using_a_search_controller
然后,删除导航控制器并使表格搜索场景成为初始视图控制器.表格仍然正常工作,但搜索控制器没有出现.
Then, deleting the navigation controller and make the Table Search Scene the initial view controller. The table still works normally, but the search controller doesn't show up.
有没有办法修改该代码,以便您可以使用 UISearchController 而无需将其嵌入到导航控制器中?我在文档中没有看到任何需要 UINavigationController 的内容.所有关于此的堆栈溢出问题都告诉您单独使用 UISearchBar.我对使用 UISearchController 特别感兴趣.
Is there a way to modify that code so that you can use UISearchController without embedding it in a navigation controller? I don't see anything in the documentation requiring UINavigationController. All of the stack overflow questions about this tell you to use the UISearchBar alone. I am specifically interested in using UISearchController.
推荐答案
你可能想多了.你可以完美地使用 UISearchController 而没有导航控制器或表格视图.人们使用表格视图的唯一原因是有一些显示搜索结果的方式很好.但是 UISearchController 是一个非常普通的视图控制器,无论如何都可以完成它的工作.这真的是世界上最简单的事情——这就是它的美妙之处.非常简单,它可以做任何事情关于搜索.
You may be over-thinking this. You could perfectly well use a UISearchController without a navigation controller or a table view. The only reason people use a table view is that it's nice to have some way of displaying the search results. But UISearchController is a perfectly ordinary view controller and just does its job regardless. It really is the simplest thing in the world — that's the beauty of it. It's so simple, it can do anything with regard to search.
这是我能想到的最小的例子.这基本上是应用程序的全部代码:
Here's the tiniest example I could think of. This is basically the entire code of the app:
class ViewController: UIViewController {
var sc : UISearchController?
override var prefersStatusBarHidden: Bool { true }
override func viewDidLoad() {
super.viewDidLoad()
let vc2 = ViewController2()
let sc = UISearchController(searchResultsController: vc2)
self.sc = sc
sc.searchResultsUpdater = vc2
let sb = sc.searchBar
self.view.addSubview(sb)
}
}
class ViewController2: UIViewController, UISearchResultsUpdating {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
}
func updateSearchResults(for searchController: UISearchController) {
guard searchController.isActive else {return}
if let t = searchController.searchBar.text, !t.isEmpty {
print("You are searching for", t)
}
}
}
这完美地说明了 UISearchController 的作用,即几乎没有.它只是通过放置一个辅助视图控制器并在用户在搜索栏中键入时发出信号来响应用户点击搜索栏.其他一切都取决于您.
That illustrates perfectly what UISearchController does, namely almost nothing. It just responds to the user tapping in the search bar by putting up a secondary view controller and signaling when the user types in the search bar. Everything else is up to you.
这篇关于在没有 UINavigationController 的情况下如何使用 UISearchController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!