Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add an action on UITextView return/search key?(swift 2)

I have a UITextView which I am using as text entry in a translation application. When the user presses return/search then I want to do some action e.g. searching for word from textView. I want to do some action like we do with IBAction.

like image 540
J.S.O Avatar asked Dec 13 '25 15:12

J.S.O


1 Answers

Set up delegate of the textView. Than add shouldChangeTextInRange for detecting of the return/search buttons and performAction for you're custom action.

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

    }
like image 112
Oleg Gordiichuk Avatar answered Dec 16 '25 23:12

Oleg Gordiichuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!