【问题标题】:how to auto-paste text in swift?如何快速自动粘贴文本?
【发布时间】:2017-06-21 23:38:22
【问题描述】:

我的表格视图中有搜索栏,当我们复制文本时,我想在搜索栏中自动搜索或自动粘贴文本

我可以吗?

是源码

导入 UIKit

类 TableViewController: UITableViewController, UISearchBarDelegate {

var listEnWord = [EnWordModel]()
var filteredWord = [EnWordModel]()
var inSearchMode = false
var dbHelper = DatabaseHelper()

override func viewDidLoad() {
    super.viewDidLoad()
    searchaWord()
    loadAllWords()

  }

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

func loadAllWords(){
    listEnWord = dbHelper.getAllEnWord()
    tableView.dataSource = self
    tableView.delegate = self
    }


func searchaWord(){
    let searchBar = UISearchBar()
    self.view.addSubview (searchBar)
    searchBar.delegate = self
    searchBar.returnKeyType = UIReturnKeyType.done
    navigationItem.titleView = searchBar
}


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    self.view.endEditing(true)
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if inSearchMode {
        return filteredWord.count
    }

    return listEnWord.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ItemMenuCell

    if inSearchMode {
        cell.enword = filteredWord[indexPath.row]

    } else {

        cell.enword = listEnWord[indexPath.row]

    }

    return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {
        inSearchMode = false
        tableView.reloadData()
        self.view.endEditing(true)

    } else {

        inSearchMode = true
        let lower = searchBar.text!.lowercased()
        filteredWord = listEnWord.filter({$0.Word?.range(of: lower, options: .anchored ) != nil})
        tableView.reloadData()
        tableView.setContentOffset(CGPoint.zero, animated: true)
        self.view.endEditing(true)
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if inSearchMode {
      Singleton.ShareInstance.enwordSelected = filteredWord[indexPath.row]
    } else {
        Singleton.ShareInstance.enwordSelected = listEnWord[indexPath.row]
    }

    let des = storyboard?.instantiateViewController(withIdentifier: "DetailController")
    navigationController?.pushViewController(des!, animated: true )
}

}

谢谢你帮助我

【问题讨论】:

    标签: ios swift tableview searchbar


    【解决方案1】:

    当然,您似乎正在寻找一种在剪贴板中自动搜索文本的方法。所以首先以这种方式注册剪贴板文本更改监听器。请注意,它仅适用于您的应用程序:

    NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged),
                                           name: NSNotification.Name.UIPasteboardChanged , object: nil)
    

    然后通过这个函数处理它

    func clipboardChanged(){
        let pasteboardString: String? = UIPasteboard.general.string
        if let theString = pasteboardString {
            print("String is \(theString)")
            // Put the string into your search bar and do the search
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多