如何在 UITextView return/search 键上添加一个动作?(swift 2)
how to add an action on UITextView return/search key?(swift 2)
我有一个 UITextView,我将其用作翻译应用程序中的文本输入。当用户按下 return/search 然后我想做一些动作,例如从 textView 搜索单词。我想像我们对 IBAction 那样做一些动作。
设置 textView
的委托。比添加 shouldChangeTextInRange
用于检测 return/search 按钮和 performAction
用于您的自定义操作。
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
performAction()
}
return true
}
func performAction() {
}
我有一个 UITextView,我将其用作翻译应用程序中的文本输入。当用户按下 return/search 然后我想做一些动作,例如从 textView 搜索单词。我想像我们对 IBAction 那样做一些动作。
设置 textView
的委托。比添加 shouldChangeTextInRange
用于检测 return/search 按钮和 performAction
用于您的自定义操作。
override func viewDidLoad() {
super.viewDidLoad()
self.textView.delegate = self
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
performAction()
}
return true
}
func performAction() {
}