使搜索栏通知viewController用户将要搜索

时间:2018-09-25 03:29:56

标签: ios swift protocols

我正在我的应用程序中实现搜索功能。该应用程序包括一个视图控制器和一个处理搜索逻辑的自定义类。此自定义类称为SearchController

我的目标是在用户即将开始搜索时使searchBar通知视图控制器(与UISearchBarDelegate方法searchBarShouldBeginEditing完全相同的行为)。

通常,您只需要在searchBarShouldBeginEditing内声明SearchController,但我试图从viewController内部调用此方法,因为我希望在此事件发生时,我的观点有所改变(因此viewController应该在处理它,而不是searchController)。

SearchController类:

class SearchController: NSObject, UISearchBarDelegate {

    let searchBar = UISearchBar()
    var searchButton = UIBarButtonItem? = nil

    /* Other irrelevant class properties */

    func setup() {
        searchBar.delegate = self
        /* other setup */
    }
}

ViewController类:

class ViewController: UIViewController {

    private let searchController = SearchController()  

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.delegate = self
        searchController.setup()
    }

    /* setup a tableview to display results... this part of the implementation works fine */
}

我省略了这两个类的大部分,因为搜索功能已经可以使用。我唯一遇到的麻烦是找到一种方法来让viewController知道用户何时开始在搜索字段中输入内容。

我尝试使viewController实现UISearchBarDelegate,但是我已经使SearchController实现UISearchBarDelegate,所以为什么不能访问viewController内部的委托方法?

我希望我明确表示自己,如有必要,我可以进一步澄清此帖子。我一直在撕头发,试图自己解决这个问题。

2 个答案:

答案 0 :(得分:1)

好吧,searchBar不能有2个委托,因此您将必须找到一种解决方法。

一种解决方法是:

protocol SearchControllerDelegate: class{
    func searchBarDidBeginEditing()
}

class SearchController: NSObject, UISearchBarDelegate {

    weak var delegate: SearchControllerDelegate?

    private let searchBar = UISearchBar()

    func setup() {
        searchBar.delegate = self
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        delegate?.searchBarDidBeginEditing()
    }

}

class ViewController: UIViewController, SearchControllerDelegate{

    var searchController = SearchController()
    func setUP(){
        self.searchController.delegate = self
    }

    func searchBarDidBeginEditing() {
        /// perform some action here
    }

}

答案 1 :(得分:1)

您可以按照rmaddy的建议使用UISearchController,实施UISearchResultsUpdating

ViewController 类:

 class ViewController: UIViewController, UISearchResultsUpdating {
    private let searchController = UISearchController(searchResultsController:  nil) 

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.searchResultsUpdater = self
        searchController.delegate = self

        ....
    }

    // Called when the search bar's text or scope has changed or when the search bar becomes first responder.
    func updateSearchResults(for searchController: UISearchController) {
        //Do something...
    }
}

或者,如果您真的想自己实现搜索栏逻辑,则可以使用闭包:

SearchController 类:

class SearchController: NSObject, UISearchBarDelegate {

    let searchBar = UISearchBar()
    var searchButton = UIBarButtonItem? = nil
    var didBeginSearching: (() -> ())?

    /* Other irrelevant class properties */

    func setup() {
        searchBar.delegate = self
        /* other setup */
    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        didBeginSearching?()
    }
}

ViewController 类:

class ViewController: UIViewController {

    private let searchController = SearchController() 

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.setup()
        searchController.didBeginSearching = { [weak self] in
            //Do something...
        }
    }
}