开发者

My UITextView is not calling any delegate methods

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.r开发者_StackOverflow中文版eturnKeyType = 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


This line:

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

Creates a UITextField, not a UITextView.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜