實例化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: