3

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

My code, I've not included the whole class, just the relevant parts. I'm not using an XIB, its all added programatically. None of my delegate methods get 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");
}

Many Thanks, -Code

1
  • Dumb question, you have tapped on the correct UITextField on the screen? May 6, 2011 at 23:46

3 Answers 3

16

This line:

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

Creates a UITextField, not a UITextView.

0
1

Simple. You created a UITextField object. That is why none of the delegate methods were not called - because they were the wrong ones. Replace your viewDidLoad with the following:

- (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 delegate functions should fire just fine.

0

If you create UITextField in .xib, then declare in .h as IBOutlet... And connect textfield's delegate to File's Owner, otherwise everything should be perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.