我一直在尋找答案,我希望你能幫助我。我沒有看到代碼中的錯誤,但由於某種原因,當我按下「搜索」按鈕時,UISearchBarDelegate不起作用。UISearchBarDelegate搜索按鈕不工作swift 3 iOS 10.3
這是我的課
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
}
}
其他代表是否被調用 –
您確定要通過不是來自Storyboard的代碼添加搜索欄嗎? – CodeChanger
其實一切正常,除了「searchBarSearchButtonClicked」 –