如何在UITextView返回/搜索键上添加操作?(swift 2)

时间:2016-03-07 08:19:23

标签: xcode swift keyboard return uitextview

我有一个UITextView,我在翻译应用程序中用作文本条目。当用户按下返回/搜索时,我想做一些动作,例如从textView中搜索单词。我想像IBAction那样做一些动作。

1 个答案:

答案 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() {

    }