jjzjj

ios - searchResultsUpdater 的其他 ViewController

coder 2024-01-29 原文

如何为 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()
}

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

最佳答案

我认为您需要将实例化的 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/

有关ios - searchResultsUpdater 的其他 ViewController的更多相关文章

  1. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  2. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  3. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby - 调用其他方法的 TDD 方法的正确方法 - 2

    我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent

  6. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  7. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. ruby-on-rails - Ruby on Rails,在更新其他列值时更改列值 - 2

    我在Rails模型中有两列相互关联:Article.bodyArticle.body_updated_on每次Article.body更新时,我想将Article.body_updated_on更改为Time.now。如果任何其他字段已更新,则无需进行任何操作。 最佳答案 只需在将回调保存到您的文章模型之前添加classArticle 关于ruby-on-rails-RubyonRails,在更新其他列值时更改列值,我们在StackOverflow上找到一个类似的问题:

  10. ruby-on-rails - 用一系列时间增量填充选择,加上其他选项 - 2

    使用RubyonRails,我使用给定的增量(例如每30分钟)用时间填充“选择”。目前我正在YAML文件中写出所有的可能性,但我觉得有一种更巧妙的方法。我想我想提供一个开始时间、一个结束时间、一个增量,并且目前只提供一个名为“关闭”的选项(想想“business_hours”)。所以,我的选择可能会显示:'Closed'5:00am5:30am6:00am...[allthewayto]...11:30pm谁能想出更好的方法,或者只是将它们全部“拼写”出来的最佳方法? 最佳答案 此答案基于@emh的答案。defcreate_hour

随机推荐