My UITextView does not call any delegate methods

My problem is that none of the delegate methods of the UITextView are called.

My code, I did not include the entire class, but only the relevant parts. I do not use XIB, all this is added programmatically. None of my delegate methods are called.

.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");
}

Thanks a lot, code

+3
source share
3 answers

This line:

messageTextField = [[UITextField alloc] initWithFrame:...

Creates a UITextField, not a UITextView.

+16
source

Simple You have created a UITextField object. This is why none of the delegate methods were called - because they were wrong. Replace viewDidLoad as follows:

- (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];
}

Compile it, and now the delegation functions should work just fine.

+1
source

UITextField .xib, .h IBOutlet... , .

0

All Articles