kri*_*orn 6 swift ios8 uisearchcontroller
我有一个UITableViewController班级,我正在实施一个UISearchController.我添加了以下代表:
class EmployeesTableView: UITableViewController, NSFetchedResultsControllerDelegate,UISearchResultsUpdating{
Run Code Online (Sandbox Code Playgroud)
我正在进口两个UIKit和CoreData.我收到以下错误:
"Type 'CustomTableViewController' does not conform to protocol UISearchResultsUpdating"
Run Code Online (Sandbox Code Playgroud)
我需要做些什么才能使控制器符合协议?
Gra*_*ing 17
斯威夫特3:
func updateSearchResults(for searchController: UISearchController) {
// code here
}
Run Code Online (Sandbox Code Playgroud)
将协议添加到类定义时,最简单的方法是将鼠标悬停在协议名称上,然后单击其名称.这将提升其定义.使用协议定义,它们通常会紧随其后的方法.如果需要一个方法,它将位于顶部,如果它在前面是可选的,那么它不是必需的以便符合.
在`UISearchResultsUpdating的情况下,它只有一个方法,它是必需的.只需复制方法或多种方法,然后单击后退箭头即可返回到您的班级.将方法粘贴到您的类中,并实现它们.如果它们是可选方法(在这种情况下没有可选方法),请从前面删除可选方法.这是我从定义中复制的内容.
func updateSearchResultsForSearchController(searchController: UISearchController)
Run Code Online (Sandbox Code Playgroud)
然后更新它以执行您想要执行的操作.
func updateSearchResultsForSearchController(searchController: UISearchController) {
//do whatever with searchController here.
}
Run Code Online (Sandbox Code Playgroud)
作为另一个例子,命令单击NSFechedResultsControllerDelegate.您将看到它没有必需的方法,但有许多可选的方法.此信息通常也可以在文档中找到,但我发现命令+单击是找到我正在寻找的内容的最快方法.
小智 6
Swift 3.0
//Make sure to import UIKit
import Foundation
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
var searchController = UISearchController()
override func viewDidLoad() {
//Setup search bar
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
//Set delegate
searchController.searchResultsUpdater = self
//Add to top of table view
tableView.tableHeaderView = searchController.searchBar
}
}
extension ViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
print(searchController.searchBar.text!)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7259 次 |
| 最近记录: |