Ios 如何在swift中自动粘贴文本?
我的表视图中有搜索栏我想在复制文本后自动搜索或在搜索栏中自动粘贴文本 我能做吗 这是源代码 导入UIKit 类TableViewController:UITableViewController、UISearchBarDelegate{Ios 如何在swift中自动粘贴文本?,ios,swift,tableview,searchbar,Ios,Swift,Tableview,Searchbar,我的表视图中有搜索栏我想在复制文本后自动搜索或在搜索栏中自动粘贴文本 我能做吗 这是源代码 导入UIKit 类TableViewController:UITableViewController、UISearchBarDelegate{ var listEnWord = [EnWordModel]() var filteredWord = [EnWordModel]() var inSearchMode = false var dbHelper = DatabaseHelper() overri
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 )
}
}
谢谢您的帮助当然,您似乎正在寻找一种在剪贴板中自动搜索文本的方法。所以,首先用这种方法注册剪贴板文本更改侦听器。请注意,它仅适用于您的应用程序:
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
}
}
当然,看起来您正在寻找一种在剪贴板中自动搜索文本的方法。所以,首先用这种方法注册剪贴板文本更改侦听器。请注意,它仅适用于您的应用程序:
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
}
}