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

时间:2018-12-31 05:28:20

标签: ios swift uinavigationcontroller uisearchcontroller

所以我有以下设置。

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

我需要解雇VC3,然后回到VC1。但是,VC2和VC3都不是导航控制器堆栈的一部分。我已经尝试了所有建议的替代方法,但无济于事。

如何将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
    }

1 个答案:

答案 0 :(得分:0)

通过以下方式解决此问题:

将情节提要Segue从VC2删除到VC3。

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

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