如何为ViewController设置另一个searchResultsUpdater
我试图这样做,但是结果没有更新。

let searchNav = storyboard!.instantiateViewController(withIdentifier: "SearchControllerNav") as! UINavigationController
    let vc = searchNav.topViewController as! PageMenuVC
    let searchVC = storyboard!.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC
    self.searchController = UISearchController(searchResultsController: vc)
    vc.searchController.searchResultsUpdater = searchVC

这是我的PageMenuVC:
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)


    var controllerArray : [UIViewController] = []

    let searchVC = storyboard?.instantiateViewController(withIdentifier: "SearchVC")
    controllerArray.append(searchVC!)

    let testVC = storyboard?.instantiateViewController(withIdentifier: "test")
    controllerArray.append(testVC!)

    // Customize menu (Optional)
    let parameters: [CAPSPageMenuOption] = [
        .scrollMenuBackgroundColor(UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0)),
        .viewBackgroundColor(UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 20.0/255.0, alpha: 1.0)),
        .selectionIndicatorColor(UIColor.orange),
        .bottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
        .menuItemFont(UIFont(name: "HelveticaNeue", size: 13.0)!),
        .menuHeight(40.0),
        .menuItemWidth(90.0),
        .centerMenuItems(true)
    ]

    // Initialize scroll menu
    pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height), pageMenuOptions: nil)

    self.addChildViewController(pageMenu!)
    self.view.addSubview(pageMenu!.view)
    pageMenu!.didMove(toParentViewController: self)

}

SearchVC中,我具有以下功能:
//MARK: Filter
func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredUsers = allUsers.filter { user in
        return user.name.lowercased().contains(searchText.lowercased())
    }
    collectionView.reloadData()
}




//MARK: SearchResultDelegate
func updateSearchResults(for searchController: UISearchController) {
    let searchBarText = searchController.searchBar.text!
    filterContentForSearchText(searchText: searchBarText)
    self.searchController = searchController
    collectionView.reloadData()
}

如果您需要更多信息,请告诉我!

最佳答案

我认为您需要将实例化的视图控制器保留在self视图控制器的( private )属性中:如果将其设置为vc.searchController.searchResultsUpdater的委托,则这只是weak引用,因此,如果您不将searchVC存储在self中,它会被过早销毁(仅在代码块的末尾)。 searchNav同样适用(但我认为这将被提出,因此我将在下面跳过):

class MyVC : UIViewController {
    var searchVC:UIViewController?
    // ...
    func XYZ() {
        let searchNav = storyboard!.instantiateViewController(withIdentifier: "SearchControllerNav") as! UINavigationController
        let vc = searchNav.topViewController as! PageMenuVC
        // Store searchVC in property:
        self.searchVC = storyboard!.instantiateViewController(withIdentifier: "SearchVC") as! SearchVC
        self.searchController = UISearchController(searchResultsController: vc)
        vc.searchController.searchResultsUpdater = searchVC
        self.present(searchNav, animated:true, completion:nil)
    }
}

关于ios - 用于searchResultsUpdater的其他ViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42218091/

10-12 16:42