【问题标题】:how to add an action on UITextView return/search key?(swift 2)如何在 UITextView 返回/搜索键上添加操作?(swift 2)
【发布时间】:2016-06-20 17:27:55
【问题描述】:

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

【问题讨论】:

    标签: xcode swift keyboard return uitextview


    【解决方案1】:

    设置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() {
    
        }
    

    【讨论】:

    • 这与 UITextView 相同,我将为您编辑它
    猜你喜欢
    • 2012-07-18
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    相关资源
    最近更新 更多