gpt4 book ai didi

ios - 在没有 Storyboard的情况下使用 UISearchController

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:48 24 4
gpt4 key购买 nike

我是 iOS 的新手(100 小时左右),我正在尝试实现一个没有 Storyboard的 UISearchController。是否可以通过编程方式执行此操作?

最佳答案

您必须实现 UISearchController以编程方式进行,因为 Apple 尚未提供使用 Storyboard 进行设置的能力。

以下是实现 UISearchController 的步骤在 View Controller 中

  • 符合UISearchResultsUpdating协议(protocol)
  • 创建一个变量来引用 UISearchController

    var searchController: UISearchController!
  • 设置searchControllerviewDidLoad(_:)

    override func viewDidLoad() {
    super.viewDidLoad()

    searchController = UISearchController(searchResultsController: nil)

    // The object responsible for updating the contents of the search results controller.
    searchController.searchResultsUpdater = self

    // Determines whether the underlying content is dimmed during a search.
    // if we are presenting the display results in the same view, this should be false
    searchController.dimsBackgroundDuringPresentation = false

    // Make sure the that the search bar is visible within the navigation bar.
    searchController.searchBar.sizeToFit()

    // Include the search controller's search bar within the table's header view.
    tableView.tableHeaderView = searchController.searchBar

    definesPresentationContext = true
    }
  • 实现方法updateSearchResultsForSearchController(_:) ,当搜索栏成为第一响应者或用户更改搜索栏内的文本时调用

    func updateSearchResultsForSearchController(searchController: UISearchController) {
    // No need to update anything if we're being dismissed.
    if !searchController.active {
    return
    }

    // you can access the text in the search bar as below
    filterString = searchController.searchBar.text

    // write some code to filter the data provided to your tableview
    }

关于ios - 在没有 Storyboard的情况下使用 UISearchController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com