实例化UITextVIew的类对象并将UITextView的实例对象的delegate设为self。 m_contentTextField = [[[UITextView alloc] init] autorelease]; m_contentTextField.frame = CGRectMake(0, 0, 320, 90) ; m_contentTextField.backgroundColor = [UIColor whiteColor] ; m_contentTextField.font = [UIFont systemFontOfSize:14]; m_contentTextField.delegate = self ; [m_contentTextField becomeFirstResponder];
之后通过UITextViewDelegate的方法中的textViewDidChange方法来监听文字改变的消息了。 - (void)textViewDidChange:(UITextView *)textView { NSLog(@"textViewDidChange:%@", textView.text); }一定要记得在头文件中导入UITextViewDelegate哦。
附加:
该代理还实现了以下几种监听事件– textViewShouldBeginEditing:
– textViewDidBeginEditing:
– textViewShouldEndEditing:
– textViewDidEndEditing: