首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISearchBarDelegate搜索按钮无法快速工作3 iOS 10.3

UISearchBarDelegate搜索按钮无法快速工作3 iOS 10.3
EN

Stack Overflow用户
提问于 2017-04-04 04:41:59
回答 1查看 1.2K关注 0票数 1

我一直在找答案,希望你能帮我。我看不到代码中的错误,但出于某种原因,当我按下"Search“按钮时,UISearchBarDelegate就不能工作了。

这是我的课

代码语言:javascript
复制
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
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 06:47:44

这只是猜测,但是shouldChangeTextIn range:方法返回返回键(字符\n)的false,这可能会影响其他委托方法。您能否尝试修改该方法以使用以下实现,并查看是否调用了searchBarSearchButtonClicked方法?

代码语言:javascript
复制
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, 
               replacementText text: String) -> Bool {
    return true
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43198422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档