UISearchBar和UISearchBarController之间的区别?

时间:2019-06-10 11:34:50

标签: ios swift xcode uisearchbar

我对UISearchBar/UISearchBarDelegateUISearchBarController之间的区别感到困惑。

现在,这就是我实现搜索栏的目的:

class SearchViewController: UIViewController {
    @IBOutlet weak var searchBar: UISearchBar!
}

extension SearchViewController: UISearchBarDelegate {

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchBar.resignFirstResponder()
        if !searchBar.text!.isEmpty {
            performSegue(withIdentifier: "Search", sender: searchBar.text!)
        }
    }
}

我看到人们在网上做类似的事情:

let searchController = UISearchController(searchResultsController: nil)

这和我在做什么有什么区别?

2 个答案:

答案 0 :(得分:1)

您的问题类似于询问“ UIViewUIViewController有什么区别?”

UISearchBar视图。它具有一个文本字段和(可选)一个按钮,仅此而已。

UISearchController controller ,它控制“搜索”的更抽象的行为。它的一项工作是控制其searchBar的行为,但它还有许多其他工作,例如告诉searchResultsUpdater它应更新搜索结果(因为搜索栏中的文本包含更改或其他)。它还公开了obscuresBackgroundDuringPresentationhidesNavigationBarDuringPresentation之类的属性,以控制当搜索栏为第一响应者时视图的外观。

如果您直接使用UISearchBar,则需要手动实现UISearchController为您完成的所有这些操作。如果您打算创建UISearchController不提供的自己的搜索行为,则可能需要这样做,但是如果您只想在应用中使用简单的旧搜索功能,请使用UISearchController,因为将为您节省很多时间。

答案 1 :(得分:0)

大约。

  • import more_itertools as mit iterable = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 20] # input x = [list(group) for group in mit.consecutive_groups(iterable)] output = [(i[0],i[-1]) if len(i)>1 else i[0] for i in x] print(output) 是在屏幕上渲染的对象的类别,(UIxxx等)

  • UIButton是协调屏幕上对象(接收事件等)的对象类。

  • UIxxxController嵌入功能代码以使UIxxxDelegate对象表现出所需方式(填充内容等)的对象类。

您的代码看起来就像您通过IB连接事物。在互联网上找到的代码看起来像控制器是直接在代码中构建的,而不是通过IB构建的; UIxxx被实例化。