Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITextView数据更改快速_Ios_Swift_Uitextview - Fatal编程技术网

Ios UITextView数据更改快速

Ios UITextView数据更改快速,ios,swift,uitextview,Ios,Swift,Uitextview,如何使用Swift检测UITextView中的数据变化?以下代码不执行任何检测 我正在声明UITextView: @IBOutlet weak var bodyText: UITextView! optional func textViewDidChange(_ textView: UITextView!) { println(bodyText.text) } 谢谢 Scott设置UITextView的委托。请参阅 在viewDidLoad bodyText!.delegate =

如何使用
Swift
检测
UITextView
中的数据变化?以下代码不执行任何检测

我正在声明
UITextView

@IBOutlet weak var bodyText: UITextView!

optional func textViewDidChange(_ textView: UITextView!) {
    println(bodyText.text)
}
谢谢
Scott

设置
UITextView
委托
。请参阅

viewDidLoad

bodyText!.delegate = self

您需要设置UITextView并在其中实现方法。不幸的是,我不知道swift文档是否在线可用。所有链接都指向objective-c文档

代码如下所示:(为SWIFT 4.2更新)

对于Swift 4:

func textViewDidChange(\utextView:UITextView){
//你的代码在这里

}
在我的例子中,我希望实现独立于UIViewController,因此我不需要仅为文本更改分配委托。或者甚至可能在UITextView上有某种验证,您希望每个字段都包含它,而不是让一个代理管理许多复杂的逻辑

它需要将UITextView子类化,但在我看来非常值得:

class TextView: UITextView {

    convenience init() {
        self.init(frame: CGRect.zero, textContainer: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChangeNotification), name: UITextView.textDidChangeNotification , object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func textDidChangeNotification(_ notif: Notification) {
        guard self == notif.object as? UITextView else {
            return
        }
        textDidChange()
    }

    func textDidChange() {
        // the text in the textview just changed, below goes the code for whatever you need to do given this event

        // or you can just set the textDidChangeHandler closure to execute every time the text changes, useful if you want to keep logic out of the class
        textDidChangeHandler?()
    }

    var textDidChangeHandler: (()->Void)?

}

此方法是否仅在文本值更改时被调用,或者它是否也响应属性文本属性(粗体/斜体/etc)中的更改?尚未测试。但该方法似乎会响应文本中的任何更改。此UITextView占位符解决方案提供了一个工作示例:
class TextView: UITextView {

    convenience init() {
        self.init(frame: CGRect.zero, textContainer: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChangeNotification), name: UITextView.textDidChangeNotification , object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func textDidChangeNotification(_ notif: Notification) {
        guard self == notif.object as? UITextView else {
            return
        }
        textDidChange()
    }

    func textDidChange() {
        // the text in the textview just changed, below goes the code for whatever you need to do given this event

        // or you can just set the textDidChangeHandler closure to execute every time the text changes, useful if you want to keep logic out of the class
        textDidChangeHandler?()
    }

    var textDidChangeHandler: (()->Void)?

}