【问题标题】:How to detect text view begin editing and end editing in swift 3如何在swift 3中检测文本视图开始编辑和结束编辑
【发布时间】:2017-01-05 02:00:28
【问题描述】:

我在objective-c中找到了相关的问题和答案,但是我找不到快速的正确方法?是否有方法可以检测与 swift 中提交的文本相同的文本视图?

我想解决通过键盘隐藏文本视图的问题。为此,我需要一种方法来检测开始和结束编辑文本视图。

任何帮助将不胜感激。 注意:我使用的是 swift 3。

【问题讨论】:

  • 使用 UITextView 的委托方法。

标签: swift textview


【解决方案1】:

正如 Bawpotter 所说,“使用 UITextView 的委托方法”。以下是它们在 Swift 3 中的样子:

func textViewDidBeginEditing(_ textView: UITextView) {

  // Run code here for when user begins type into the text view

}

func textViewDidEndEditing(_ textView: UITextView) {

  // Run code here for when user ends editing text view

}

还要确保您的 UITextView 的委托设置为 self 或这些方法在您的应用中的任何位置

【讨论】:

    【解决方案2】:

    使您的 ViewController 类符合 UITextViewDelegate 然后编写 textView.delegate = self 在您的 viewDidLoad() 中。之后为您的 textView 编写委托方法:

    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
         //  your code here
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        //  your code here
    }
    
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
         //  your code here
    }
    
    func textViewDidEndEditing(_ textView: UITextView) { 
       // your code here
    }
    

    【讨论】:

    猜你喜欢
    • 2010-09-25
    • 2011-05-07
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2014-04-18
    • 2013-07-16
    • 1970-01-01
    相关资源
    最近更新 更多