我的问题是没有调用UITextView的委托方法。
我的代码,我没有包括整个班级,只包括相关部分。 我没有使用XIB,它全部以编程方式添加。 我的委托方法都没有被调用。
·H
@interface ChatAppViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, UITextViewDelegate>
{
UITextView * messageTextField;
}
的.m
- (void)viewDidLoad
{
messageTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 408, 310, 70)];
messageTextField.textColor = [UIColor blackColor]; //text color
messageTextField.font = [UIFont systemFontOfSize:17.0]; //font size
[messageTextField setPlaceholder:@"Post a message" ];
messageTextField.backgroundColor = [UIColor whiteColor]; //background color
messageTextField.autocorrectionType = UITextAutocorrectionTypeNo;
messageTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
messageTextField.returnKeyType = UIReturnKeyDone; // type of the return key
messageTextField.delegate = self;
messageTextField.hidden = NO;
[self.view addSubview:messageTextField];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
NSLog(@"textViewShouldBeginEditing");
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
NSLog(@"textViewDidBeginEditing");
}
- (void)textViewDidChange:(UITextView *)textView {
NSLog(@"textViewDidChange");
}
- (void)textViewDidChangeSelection:(UITextView *)textView {
NSLog(@"textViewDidChangeSelection");
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
NSLog(@"textViewShouldEndEditing");
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
NSLog(@"textViewDidEndEditing");
}
非常感谢, -code
答案 0 :(得分:16)
这一行:
messageTextField = [[UITextField alloc] initWithFrame:...
创建UITextField,而不是UITextView。
答案 1 :(得分:1)
简单。您创建了一个UITextField对象。这就是没有调用任何委托方法的原因 - 因为它们是错误的。用以下内容替换viewDidLoad:
- (void)viewDidLoad
{
messageTextField = [[UITextView alloc] initWithFrame:CGRectMake(5, 408, 310, 70)];
messageTextField.textColor = [UIColor blackColor]; //text color
messageTextField.font = [UIFont systemFontOfSize:17.0]; //font size
messageTextField.backgroundColor = [UIColor whiteColor]; //background color
messageTextField.autocorrectionType = UITextAutocorrectionTypeNo;
messageTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
messageTextField.returnKeyType = UIReturnKeyDone; // type of the return key
messageTextField.delegate = self;
messageTextField.hidden = NO;
[self.view addSubview:messageTextField];
}
编译它,现在委托函数应该正常启动。
答案 2 :(得分:0)
如果您在UITextField
中创建.xib
,则在.h
中声明为IBOutlet
...并将文本字段的委托连接到文件所有者,否则一切都应该是完美的。< / p>