我有一个UITextView,我在翻译应用程序中用作文本条目。当用户按下返回/搜索时,我想做一些动作,例如从textView中搜索单词。我想像IBAction那样做一些动作。
答案 0 :(得分:6)
设置textView
的委托。比添加shouldChangeTextInRange
用于检测返回/搜索按钮,添加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() {
}