Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView not becoming first responder

I have a UIView that contains a UITextView. The UIView is initiated inside a UIViewController.

But when I touch the UITextView box, nothing happens. Neither the keyboard appears nor delegate methods respond to interaction.

Code:

    noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
    noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
    noteText.layer.borderWidth = 2;
    [noteText setEditable:YES];
    noteText.userInteractionEnabled = YES;
    noteText.returnKeyType = UIReturnKeyDone;
    noteText.font = [UIFont fontWithName:@"Calibri" size:16];
    noteText.textColor = [UIColor blackColor];
    [self addSubview:noteText];

Update 1

I removed all other views from the UIViewController, and only put a UITextView in the UIViewController and still not reacting. Neither cursor or keyboard appear.

like image 257
lagos Avatar asked Oct 01 '12 21:10

lagos


4 Answers

noteText = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 700, 240)];
noteText.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2].CGColor;
noteText.layer.borderWidth = 2;
[noteText setEditable:YES];
noteText.userInteractionEnabled = YES;
noteText.returnKeyType = UIReturnKeyDone;
noteText.font = [UIFont fontWithName:@"Calibri" size:16];
noteText.textColor = [UIColor blackColor];
[self addSubview:noteText];

// Two suggestions

self.userInteractionEnabled = YES; // superview may blocks touches
self.clipsToBounds = YES; // superview may clips your textfield, you will see it
like image 87
NeverBe Avatar answered Nov 18 '22 08:11

NeverBe


I found the error.

In one class I categorize the UITextView instead of subclass and set canBecomeFirstResponder to NO.

like image 35
lagos Avatar answered Nov 18 '22 08:11

lagos


try this: in your .h file conform the delegate UITextViewDelegate

:<UITextViewDelegate>

in your .m file:

noteText.delegate = self;

And delegate methods:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    [textView becomeFirstResponder];
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    //resign for exapmple
    return YES;
}

Hope this help!

like image 3
donjordano Avatar answered Nov 18 '22 09:11

donjordano


I know it's late, but maybe for someone it'll be helpful. I had the same problem, and it disappeared after I used

[self setSelectable:true];

in my UITextView subclass.

like image 3
Artem Avatar answered Nov 18 '22 08:11

Artem