gpt4 book ai didi

ios - Swift:SearchBar 在parentViewController 上保持可见

转载 作者:行者123 更新时间:2023-11-30 13:54:13 26 4
gpt4 key购买 nike

我正在使用 searchBar ,当我选择搜索时,它工作得很好(除了我无法修复 Attempting to load the view of a view controller while it is deallocating... UISearchController 的运行时警告)

但是,如果 searchBar 处于事件状态并且我从 NavBar 按“后退”,则会显示parentView,但上一屏幕中的 searchBar 也可见。如果按下“后退”按钮,我尝试关闭搜索栏,但它仍然可见一段时间。我正在考虑

self.navigationItem.backButtonItem.enabled = false  

虽然 searchBar 处于事件状态,但我不喜欢这个解决方案

我的代码(我也有 func updateSearchResultsForSearchController):

class ViewController: UIViewController ,UISearchResultsUpdating {
var resultSearchController = UISearchController()

override func viewDidLoad() {
super.viewDidLoad()

if #available(iOS 9.0, *) {
self.resultSearchController.loadViewIfNeeded()// iOS 9
} else {
// Fallback on earlier versions
let _ = self.resultSearchController.view// iOS 8
}
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.hidesNavigationBarDuringPresentation = false
self.tableView.tableHeaderView = controller.searchBar
return controller
})()

运行时警告(不确定这是否是这里的问题)

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController: 0x7ff88130fdb0>)

最佳答案

要删除警告,请更改以下代码:

var resultSearchController = UISearchController()

至:

var resultSearchController : UISearchController!

并在 viewWillDisappear 方法中关闭您的 resultSearchController

关于ios - Swift:SearchBar 在parentViewController 上保持可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855060/

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