lagos
lagos

Reputation: 1968

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.

Upvotes: 6

Views: 6638

Answers (8)

Artem
Artem

Reputation: 401

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.

Upvotes: 3

Danil
Danil

Reputation: 1780

Check this things:

  1. add your viewcontroller on the right UIWindow.

  2. set window makeKeyAndVisible and add rootViewController to window at this method:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    I think here is something wrong in xib file or at appdelegate.

Upvotes: 2

lagos
lagos

Reputation: 1968

I found the error.

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

Upvotes: 4

NeverBe
NeverBe

Reputation: 5038

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

Upvotes: 7

iDev
iDev

Reputation: 23278

Whenever something like this happens check the following items,

  1. Make sure that the Text view is properly retained and released only in dealloc.(If you are not using ARC)
  2. Check the frame of both Text view and it's parent views. Make sure that the subview is fitting exactly inside the parent view. Also make sure that this is the case with all the superviews of text view.
  3. Check whether the delegate is set properly.

If all these are done, try adding a button on top of text view and check if it's target selector is getting called. If yes, the issue is with text view delegate or release statement. Otherwise the issue is with frame setting of Text view or it's superviews.

Upvotes: 1

donjordano
donjordano

Reputation: 346

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!

Upvotes: 3

Anton K
Anton K

Reputation: 344

Maybe you have some mistake in your overriden -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method? Maybe you do resignFirstResponder there or put some view above all?

Upvotes: 1

Daddy
Daddy

Reputation: 9035

Try overriding - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { inside the UIView to see if it's receiving touches

Just in case, try setting userInteractionEnabled = YES; for your UIView object. If it's somehow set as NO it will trickle down to all of its subviews

Upvotes: 1

Related Questions