gpt4 book ai didi

iOS Swift 4 - 如何在 NavigationController 中推送 UISearchController

转载 作者:行者123 更新时间:2023-11-30 10:55:35 25 4
gpt4 key购买 nike

所以我有以下设置。

InitialViewController(VC) -> NavigationController -> ViewController1(VC1) 包含一个 UISearchController,在新 Controller ViewController2(VC2) 中显示结果 -> 从单元格中单击 VC2 -> 启动 ViewController3 (VC3)

我需要关闭 VC3 并返回 VC1。但是,VC2 和 VC3 都不属于导航 Controller 堆栈的一部分。我几乎尝试了所有建议的替代方案,但没有效果。

如何将 UISearchcontroller 和结果推送到导航堆栈?

我在 VC1 中实例化我的 UISearchController,如下所示:

    searchController = UISearchController(searchResultsController: 
searchResultsController)
searchController!.searchResultsUpdater = searchResultsController
searchController?.delegate = self
searchController!.obscuresBackgroundDuringPresentation = true
searchController!.searchBar.placeholder = "Search stocks"
searchController?.hidesNavigationBarDuringPresentation = false
definesPresentationContext = true
searchController!.searchBar.delegate = searchResultsController
//navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
navigationItem.titleView = searchController?.searchBar
definesPresentationContext = true

还有我的 searchResultsController:

extension WatchListTableViewController: UISearchResultsUpdating, UISearchBarDelegate {
// MARK: - UISearchResultsUpdating Delegate
func updateSearchResults(for searchController: UISearchController) {
// TODO
filterContentForSearchText(searchController.searchBar.text!)
}
}
class WatchListTableViewController: UITableViewController {
var stocks = [String]()
var filteredStocks = [String]()

override func viewDidLoad() {
super.viewDidLoad()
stocks = ["Apple", "Google", "Microsoft", "Tesla"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}

最佳答案

通过以下方式解决了这个问题:

删除了从 VC2 到 VC3 的 Storyboard Segue。

在 VC2 中 - 使用以下方法将 VC3 推送到导航堆栈:

self.presentingViewController?.navigationController?.pushViewController(vc, animated:true)

关于iOS Swift 4 - 如何在 NavigationController 中推送 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53983908/

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