• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - searchResultsUpdater 的其他 ViewController

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:57:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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()
}

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



Best Answer-推荐答案


我认为您需要将实例化的 View Controller 保留在 self View Controller 的(私有(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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap