gpt4 book ai didi

ios - 显示带有操作的搜索栏(栏项)

转载 作者:搜寻专家 更新时间:2023-10-30 22:25:08 25 4
gpt4 key购买 nike

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

我的代码在这里:

// 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()
}
}

当我点击按钮时,没有任何反应(只在控制台打印文本),我想要的是下图:

Show search

最佳答案

您的类(class)需要符合 UISearchBarDelegate ,我不确定你是否已经这样做了。还要确保呈现搜索 View

来自 Apple 的文档:

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.

这是我的应用中的示例

@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)
}

关于ios - 显示带有操作的搜索栏(栏项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729151/

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