I have two UITextView
's in my iPhone app. When the user selects one UITextView
the controller automatically goes to another UITextView
. I have tried the following way in my project. But the second UITextView
does not becomefirstresponder
until the user clicks inside the second UITextView
.
-(void) viewDidLoad
{
TextView1 = [[UITextView alloc] initWithFrame:CGRectMake(10, 320, 300, 50)];
TextView1.delegate = self;
TextView1.backgroundColor = [UIColor clearColor];
TextView1.layer.borderWidth = 2;
TextView1.layer.borderColor = [UIColor blackColor].CGColor;
TextView1.layer.cornerRadius = 5;
[self.view addSubview: TextView1];
TextView2 = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, 300, 30)];
TextView2.delegate = self;
TextView2.backgroundColor = [UIColor whiteColor];
TextView2.layer.borderWidth = 2;
TextView2.layer.borderColor = [UIColor lightTextColor].CGColor;
TextView2.layer.cornerRadius = 5;
[self.view addSubview: TextView2];
UIBarButtonItem *textBarButton = [[UIBarButtonItem alloc] initWithCustomView: TextView2];
accessoryToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
accessoryToolBar.items = [NSArray arrayWithObjects:textBarButton, nil];
}
-(BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
NSLog(@"TextView Should Begin Editing");
if (textView == messageTextView)
{
[TextView2 setInputAccessoryView:accessoryToolBar];
[TextView1 resignFirstResponder];
[TextView2 becomeFirstResponder];
}
else
{
[TextView2 setInputAccessoryView:accessoryToolBar];
[TextView1 resignFirstResponder];
}
return YES;
}
How can I fix this?
Remove the complete textViewShouldBeginEditing
method. That's the wrong point.
You want, that as soon, as the user touches the first textView, the second one should be selected, right? If that is what you want, use textViewDidBeginEditing:
instead.
- (void) textViewDidBeginEditing:(UITextView *)textView
{
if(textView == TextView1) {
[TextView2 becomeFirstResponder];
}
}
That should be everything you need.