菜鸟教程小白 发表于 2022-12-11 18:57:58

ios - searchResultsUpdater 的其他 ViewController


                                            <p><p>如何为 <code>searchResultsUpdater</code> 设置另一个 <code>ViewController</code>?
我试着这样做,但结果没有更新。</p>

<pre><code>let searchNav = storyboard!.instantiateViewController(withIdentifier: &#34;SearchControllerNav&#34;) as! UINavigationController
    let vc = searchNav.topViewController as! PageMenuVC
    let searchVC = storyboard!.instantiateViewController(withIdentifier: &#34;SearchVC&#34;) as! SearchVC
    self.searchController = UISearchController(searchResultsController: vc)
    vc.searchController.searchResultsUpdater = searchVC
</code></pre>

<p>这是我的 PageMenuVC:</p>

<pre><code>override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)


    var controllerArray : = []

    let searchVC = storyboard?.instantiateViewController(withIdentifier: &#34;SearchVC&#34;)
    controllerArray.append(searchVC!)

    let testVC = storyboard?.instantiateViewController(withIdentifier: &#34;test&#34;)
    controllerArray.append(testVC!)

    // Customize menu (Optional)
    let parameters: = [
      .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: &#34;HelveticaNeue&#34;, 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)

}
</code></pre>

<p>在 <code>SearchVC</code> 我有这个功能:</p>

<pre><code>//MARK: Filter
func filterContentForSearchText(searchText: String, scope: String = &#34;All&#34;) {
    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()
}
</code></pre>

<p>如果您需要更多信息,请告诉我!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您需要将实例化的 ViewController 保留在 <code>self</code>ViewController 的(私有(private))属性中:如果将其设置为 <code>vc.searchController.searchResultsUpdater</code>,这只是一个 <code>weak</code> 引用,因此如果你不将 <code>searchVC</code> 存储在 <code>self</code> 中,它会很快被销毁(只是在代码块的末尾)。 searchNav 也是如此(但我认为这将被呈现,因此我将在下面跳过它):</p>

<pre><code>class MyVC : UIViewController {
    var searchVC:UIViewController?
    // ...
    func XYZ() {
      let searchNav = storyboard!.instantiateViewController(withIdentifier: &#34;SearchControllerNav&#34;) as! UINavigationController
      let vc = searchNav.topViewController as! PageMenuVC
      // Store searchVC in property:
      self.searchVC = storyboard!.instantiateViewController(withIdentifier: &#34;SearchVC&#34;) as! SearchVC
      self.searchController = UISearchController(searchResultsController: vc)
      vc.searchController.searchResultsUpdater = searchVC
      self.present(searchNav, animated:true, completion:nil)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - searchResultsUpdater 的其他 ViewController,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42218091/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42218091/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - searchResultsUpdater 的其他 ViewController