UITextView Delegate未触发shouldChangeTextInrange委托方法

时间:2016-08-26 21:43:49

标签: ios swift delegates uitextview uitextviewdelegate

我从故事板连接了UITextView,称之为txtView。在我的ViewController中,我将代理设置为txtView.delegate = self

视图控制器会触发textViewDidBeginEditingtextViewDidChange之类的方法,但不会触发func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool

是的,我确实在我的视图控制器中设置了delegate

是的,我确实将UITextViewDelegate放入了我的班级声明

不,我没有将UITextViewUITextField混淆。

有什么想法吗?谢谢!

class ViewController: UIViewController, UITextViewDelegate, UIGestureRecognizerDelegate {
  @IBOutlet weak var txtView: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()
    txtView.delegate = self
    txtView.becomeFirstResponder()  
    txtView.text = "txt"
  }

  func textViewDidChange(_ textView: UITextView) {
    print("THIS PRINTS")
  }

  func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    print("WHY DOESN'T THIS GET CALLED?")
    return true
  }
}

0 个答案:

没有答案