gpt4 book ai didi

ios - UISearchController 在关闭时清除 UISearchBar 文本

转载 作者:可可西里 更新时间:2023-11-01 04:43:16 25 4
gpt4 key购买 nike

UITableViewController 的自定义子类的 viewDidLoad 中,我将 navigationItem.titleView 设置为 searchBar UISearchController,我用 nilsearchResultsController 初始化了它。

当我点击搜索栏,输入文本,然后点击变暗的底层内容时,UISearchController 被停用(如预期的那样)并且我输入的搜索文本被清除(意外的)。

当我明确(以编程方式)将搜索 Controller 设置为不活动时,搜索文本也被清除(也是意外地)。为了解决这个问题,我之前存储了搜索文本,并在关闭搜索 Controller 后将其重新设置。虽然有点笨拙,但它确实有效。

let searchText = searchBar.text // #hack
searchController.isActive = false
searchBar.text = searchText // #hack

当搜索 Controller 自动关闭时,我使用 UISearchControllerDelegate 方法尝试了类似的操作,即将搜索文本存储在 willDismissSearchController(_:) 中并设置它回到 didDismissSearchController(_:)。但是,此解决方案仍然显示搜索文本被清除,然后恢复到之前的状态。

当搜索 Controller 自动关闭时,如何无缝地保持搜索栏文本不变?

最佳答案

对于显式(以编程方式)关闭搜索 Controller ,UISearchController 是一个 View Controller ,因此您只需调用 dismiss(animated:completion:) 而不是设置 isActive = false 并重置搜索文本:

searchController.dismiss(animated: true)

搜索栏文本在关闭后仍会保留在搜索栏中。

关于ios - UISearchController 在关闭时清除 UISearchBar 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41404739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com