Question

I am working on a message based iPhone app. I have a screen to reply to a received message. This screen contains two UITextViews like bottomTextView and topTextView.

topTextView is added as bottomTextView's InputAccessory view.

When the user enter into the screen topTextView has to becomeFirstResponder. It is showing but the cursor is not placed in the topTextView. The cursor is located in the textview bottomTextView. How to make topTextView become first responder with cursor in place?

Here is the code i have tried:

- (void)viewDidLoad
{
    [super viewDidLoad];            
    bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
    bottomBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
    [bottomBarView addSubview: bottomBarViewImage];
    [self.view addSubview: bottomBarView];

    bottomTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
    bottomTextView.delegate = self;
    bottomTextView.backgroundColor = [UIColor clearColor];
    bottomTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
    bottomTextView.scrollEnabled = NO;
    [bottomBarView addSubview: bottomTextView];

    topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
    topBarViewImage.image = [UIImage imageNamed: @"toolbarbg~iphone.png"];
    [topBarView addSubview: topBarViewImage];

    topTextView = [[UITextView alloc] initWithFrame:CGRectMake(35, 7.5, 210, 25)];
    topTextView.delegate = self;
    topTextView.backgroundColor = [UIColor clearColor];
    topTextView.font = [UIFont fontWithName:@"Helvetica" size:14];
    topTextView.scrollEnabled = NO;
    [topBarView addSubview: topTextView];

    [bottomTextView becomeFirstResponder];
    [bottomTextView setInputAccessoryView: topBarView];
}

-(void) textViewDidBeginEditing:(UITextView *)textView
{
    if(textView == bottomTextView)  
    {
        bottomTextView.scrollEnabled = NO;

        [topTextView becomeFirstResponder];
    }
}

The topTextView with topBarView is showing but the cursor is not placed in topTextView. Could you please help me to solve this issue? Thanks in advance.

Was it helpful?

Solution

I think it may be because you call [topTextView becomeFirstResponder]; in UITextView's delegate textViewDidBeginEditing:. So the topTextView only becomes first responder when you start editing bottomTextView. Try calling [topTextView becomeFirstResponder]; instead of [bottomTextView becomeFirstResponder]; in viewDidLoad. See how it goes. I'm not sure, but becomeFirstResponder may not call textViewDidBeginEditing:. Not sure it'll work, but worth the try...

EDIT :

I found a related problem here. It may be because the textView does not appear right away, so it can not become first responder. Here is the accepted answer, by @Tom :

My solution: check when the keyboard (and thus the accessory view) appeared!

Step 1) Listen for the notification (make sure this code is read before you want to receive the notification).

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(changeFirstResponder)
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];

Step 2) When the keyboard has appeared, you can set the textfield in your inputaccessoryview to become first responder:

-(void)changeFirstResponder
{
    [textField becomeFirstResponder]; //will return TRUE;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top