UISearchBarDelegate 搜索按钮不起作用 swift 3 iOS 10.3
UISearchBarDelegate search button don't work swift 3 iOS 10.3
我一直在寻找答案,希望你能帮助我。我没有看到代码中的错误,但出于某种原因,当我按下 "Search" 按钮时,UISearchBarDelegate 无法正常工作。
这是我的class
class BusquedaViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var searchBar: UISearchBar!
var referencias: [Reference]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
searchBar.tintColor = UIColor.white
searchBar.delegate = self
self.parent?.navigationItem.titleView = searchBar
referencias = DataManager.sharedInstance.obtenerReferencias()
}
override func viewWillAppear(_ animated: Bool) {
self.parent?.title = "Busqueda"
}
//MARK: UISearchBar
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var placa = searchBar.text
if text.isEmpty {
return true
} else {
let uper = text.uppercased()
let allowed = "1234567890ABCDEFGHIJKLMNÑOPQRSTUVWXYZ-"
if !allowed.contains(uper) || (placa?.characters.count)! > 9 {
return false
}
searchBar.text = searchBar.text! + uper
return false
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
}
//MARK: UITableView
func numberOfSections(in tableView: UITableView) -> Int {
return referencias.isEmpty ? 1 : referencias.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if referencias.isEmpty {
//cell.textLabel?.text = NSLocalizedString("NO_REFERENCIAS", comment: "")
} else {
cell.textLabel?.text = referencias[indexPath.row].model
}
return cell
}
}
这只是一个猜测,但是 shouldChangeTextIn range:
方法对于 return 键(字符 \n
)是 returning false,这可能会影响其他委托方法。您可以尝试修改方法以使用以下实现,并查看是否调用了 searchBarSearchButtonClicked
方法?
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool {
return true
}
我一直在寻找答案,希望你能帮助我。我没有看到代码中的错误,但出于某种原因,当我按下 "Search" 按钮时,UISearchBarDelegate 无法正常工作。
这是我的class
class BusquedaViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var searchBar: UISearchBar!
var referencias: [Reference]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: 200, height: 20))
searchBar.tintColor = UIColor.white
searchBar.delegate = self
self.parent?.navigationItem.titleView = searchBar
referencias = DataManager.sharedInstance.obtenerReferencias()
}
override func viewWillAppear(_ animated: Bool) {
self.parent?.title = "Busqueda"
}
//MARK: UISearchBar
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var placa = searchBar.text
if text.isEmpty {
return true
} else {
let uper = text.uppercased()
let allowed = "1234567890ABCDEFGHIJKLMNÑOPQRSTUVWXYZ-"
if !allowed.contains(uper) || (placa?.characters.count)! > 9 {
return false
}
searchBar.text = searchBar.text! + uper
return false
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.endEditing(true)
}
//MARK: UITableView
func numberOfSections(in tableView: UITableView) -> Int {
return referencias.isEmpty ? 1 : referencias.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if referencias.isEmpty {
//cell.textLabel?.text = NSLocalizedString("NO_REFERENCIAS", comment: "")
} else {
cell.textLabel?.text = referencias[indexPath.row].model
}
return cell
}
}
这只是一个猜测,但是 shouldChangeTextIn range:
方法对于 return 键(字符 \n
)是 returning false,这可能会影响其他委托方法。您可以尝试修改方法以使用以下实现,并查看是否调用了 searchBarSearchButtonClicked
方法?
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool {
return true
}