显示带动作的搜索栏(条形项) [英] Show search bar with action (bar item)

查看:141
本文介绍了显示带动作的搜索栏(条形项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的搜索栏有问题。我需要在右上角创建一个带有搜索按钮的表格视图,当我点击它时,它应该显示搜索栏。

I have a problem with search bar. I need to create a table view with a search button in the right corner, and when I click on it, it should show the search bar.

我的代码在这里:

// Search controller
searchController = ({
    let controller = UISearchController(searchResultsController: nil)
    controller.delegate = self
    controller.searchBar.delegate = self
    controller.searchResultsUpdater = self
    controller.dimsBackgroundDuringPresentation = false         
    controller.hidesNavigationBarDuringPresentation = true
    controller.searchBar.sizeToFit()
    return controller
})()

这是行动:

// Search action
@IBAction func search(sender: UIBarButtonItem) {
    print("Open search")
    searchController.active = true
    if searchController.searchBar.isFirstResponder() == false {
        searchController.searchBar.becomeFirstResponder()
    }
}

当我点击按钮时,什么也没有发生(仅在控制台中打印文本),我想要的是在下图中:

When I click on the button, nothing happens (only prints text in console), and what I want is in the image below:

推荐答案

您的课程需要符合 UISearchBarDelegate ,我不确定你是否已经这样做了。另外请确保您出示搜索视图

Your class needs to conform to a UISearchBarDelegate, I'm not sure if you've done that already. Also make sure you present the search view

来自Apple的文档:

From Apple's Docs:


UISearchBarDelegate协议定义了为实现UISearchBar控制功能而实现的可选方法。 UISearchBar对象为条形图上的搜索字段提供用户界面,但应用程序负责在按下按钮时实现操作。在文本字段中输入文本时,代表至少需要执行实际搜索。

The UISearchBarDelegate protocol defines the optional methods you implement to make a UISearchBar control functional. A UISearchBar object provides the user interface for a search field on a bar, but it’s the application’s responsibility to implement the actions when buttons are tapped. At a minimum, the delegate needs to perform the actual search when text is entered in the text field.

以下是我的应用中的示例

Here's a sample from my app

@IBAction func searchAction(sender: UIBarButtonItem) {
    // Create the search controller and specify that it should present its results in this same view        
    searchController = UISearchController(searchResultsController: nil) 

    // Set any properties (in this case, don't hide the nav bar and don't show the emoji keyboard option)
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.searchBar.keyboardType = UIKeyboardType.ASCIICapable

    // Make this class the delegate and present the search
    self.searchController.searchBar.delegate = self
    presentViewController(searchController, animated: true, completion: nil)
}

这篇关于显示带动作的搜索栏(条形项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆