Focus of cursor is not shifting to next UITextView in ios

531 Views Asked by At

This is my First question on ios

I am using two UITextView objects (textView1 and textView2) in a View, Each of them has some character limit with some following Scenario:

  • Initially user can only enter into textView1.
  • When the entered character limit of textView1 is over, the cursor will automatically shift to textView2.
  • After building the project, If user tap the textView2 and try to write into it, Cursor must shifted to textView1 (because it is empty).

I wrote the code and everything works fine except the third scenario, User can only enter into textView1 but focus is still on textView2

Steps to reproduce:

  • Build the project
  • user tap the textView2 first and try to write something.
  • According to written code, Focus remain in textView2 but user are writing into textView1 (see the attachment)

Here is the snapshot:

enter image description here

Here is the written code:

- (void)viewDidLoad
{            
[super viewDidLoad];
[self.textView1 becomeFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSInteger restrictedLengthForTextView2 = 130;
NSString *temp=textView.text;
if(textView == self.textView1){
    if([[textView text] length] > restrictedLengthForTextView1){
        textView.text=[temp substringToIndex:[temp length]-1];
        [textView resignFirstResponder];
        [self.textView2 becomeFirstResponder];
    }
}else{
    if([[textView text] length] > restrictedLengthForTextView2){
        textView.text=[temp substringToIndex:[temp length]-1];
        [self.textView2 resignFirstResponder];
    }

}}

- void()textViewDidBeginEditing:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;

NSLog(@"dalknwdlakwd");
if([[self.textView1 text] length] < restrictedLengthForTextView1){
    if(textView == self.textView2){
        [self.textView2 resignFirstResponder];
        [self.textView1 becomeFirstResponder];
    }   
}}

Please help me here..

3

There are 3 best solutions below

0
On

It is a known bug, with resigning and becoming first responder within the same runloop. Try the following

[textView2 resignFirstResponder];

[textView1 performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
0
On

please do as per following:

in .h file

@interface ViewController : UIViewController<UITextViewDelegate>
{
    IBOutlet UITextView *txtView1;
    IBOutlet UITextView *txtView2;
}
@end

in .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [txtView1 becomeFirstResponder];
    txtView2.editable=NO;
}

implement the textView delegate method like below:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if(textView.tag==1)
    {
        if([textView.text length]>25)
        {
            txtView2.editable=YES;
            [txtView2 becomeFirstResponder];
        }
    }
    return YES;
}

i have taken text length limit in first textview as 24 characters as an example. i hope this will help you.

0
On

An update to Annadurai's answer: it's still a bug in 2016! After trying others' suggestions to deselect the surrounding tableview cell, and/or change the tint color only this answer worked for me:

[textView1 performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];

FYI My case was that I was resigning a textView, reloading the table, and returning to the same textView. Before this fix - no cursor on the reloaded textView (Yes I could type and see characters.) After the performSelector... afterDelay - the cursor was visible again.