UITextFieldDelegate stops working after first activation of UITextField
I have a UITextField which I made here:
text_field = [[UITextField alloc] initWithFrame:CGRectMake(10, 8, 260, 40)];
text_field.delegate = [[MessageInputDelegate alloc] init];
The delegate implementation:
@implementation MessageInputDelegate
- (BOOL) textFieldShouldReturn: (UITextField *) text_field{
[the_view5 becomeFirstResponder];
the_view5->text_area.frame = CGRectMake(20, 320, 280, 40);
the_view5->message_label.frame = CGRectMake(0, 0, 320, 320);
text_field.enabled = NO;
text_field.text = @"";
return YES;
}
- (void) textFieldDidBeginEditing: (UITextField *) textField{
printf("DID CALL EDIT METHOD\n");
the_view5->text_area.frame = CGRectMake(20, 140, 280, 40);
the_view5->message_label.frame = CGRectMake(0, 0, 320, 140);
}
- (BOOL) textField: (UITextField *) textField shouldChangeCharactersInRange: (NSRange) range replacementString: (NSString *) string{
if (textField.text.length >= 400 && range.length == 0){
return NO;
}
return YES;
}
@end
It works the first time I activate the text field bu开发者_StackOverflowt not the second time...?
Thankyou.
If you're cerating text_field
from yout MessageInputDelegate
, you should use
text_field.delegate = self;
and declare delegate for both UITextFields.
If yoy create it from other class, you should declare a MessageInputDelegate
instance somewhere in your .h
file, e.g
MessageInputDelegate *mid;
and then
text_field.delegate = mid;
I used endEditing to close the keyboard and it works.
精彩评论