UITextView not becoming first responder
Asked Answered
Y

8

6

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.

Yetta answered 1/10, 2012 at 21:13 Comment(15)
Make sure "user interaction enabled" is checked on view attributes at interface builder.Alexandria
I don't use IB, but I have setted [textview setUserInteractionEnabled:YES] and [textview setEnabled]Yetta
did you tried [textview setDelegate:self]; ?Alexandria
What are the dimensions of its superview?Betty
The textview will open the keyboard automatically when you touch it, that should react even that I don't have set the delegate. But I have set the delegate from the UIViewController. Since the must part of the logical of the app happen in the uiviewcontroller.Yetta
@CarlVeazey the dimension of the UIView is 40, 80, 750, 250. So is sligthly bigger. The border of the textview is shown inside the size of the UIView.Yetta
Did you add another subview to the UITextView's superview after the text view is added?Briggs
Not in the UIView. The UIView contains only this UITextView. But the UIViewController has anothers subviews, but not in that frame. And the switching between subviews is controlled by a custom segmentedcontroller and all the other views is hidden when is not selected.Yetta
Seems like there could be another view's frame on top of the textView that's intercepting touch events.Pitts
Try subclassing the text view and overriding touchesBegan: Then try subclassic the superview and in its touchesBegan: try to find which view is getting the touches using hitTest: ?Undershorts
Now I have had remove all the others subview from superview, I'd subclassed the textview and overwritten touchesBegan:withEvent: class, and overwritten the same method in the UIView. Nothing works. I even bring it to the front. If I put some text string in the text appears, but I cant write.Yetta
what size of UIView that contains a UITextfield?Nuncupative
The UIView have the frame coordinates: 40, 80, 750, 250 and the frame of the UITextview is 0,0,700,240Yetta
Are you releasing the UITextView after adding as subview of UIView? Try commenting out that portion and check it. Could be a release issue also.Tamasha
I use ARC. So I don't need to release.Yetta
Y
4

I found the error.

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

Yetta answered 10/10, 2012 at 9:26 Comment(0)
N
7
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
Nuncupative answered 10/10, 2012 at 8:44 Comment(3)
In my case it was exactly that, the UITextField superview was being resized by autolayout but I couldn't notice it because it wasn't clipping to bounds.Foreconscious
In my case it was also that AutoLayout compressed the view, but because clipsToBounds was No, it showed anyway. Thanks.Jokjakarta
for me adding textView.userInteractionEnabled = true textView.editable = true worked, I thought this was default behavior, guess it's possible it was getting set somewhere I'm unaware ofJacobinism
Y
4

I found the error.

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

Yetta answered 10/10, 2012 at 9:26 Comment(0)
V
3

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!

Virtuous answered 9/10, 2012 at 10:28 Comment(1)
the builtin becomeFirstResponder calls textFieldShouldBeginEditing: so this will cause an infinite loopWhitehall
M
3

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.

Multifoil answered 24/11, 2016 at 9:13 Comment(0)
J
2

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.

Juggins answered 8/10, 2012 at 5:3 Comment(1)
I don't know what you means here. Is not a problem to load the rootviewcontroller in UIWindow. Neither do I use xib-files.Yetta
P
1

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

Pitts answered 4/10, 2012 at 12:50 Comment(1)
I have overwritten the method, but is not responding.Yetta
D
1

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?

Denmark answered 8/10, 2012 at 9:34 Comment(8)
For the moment the method only looks like this for see if the view react: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"Received Touch!"); } Yetta
Do you use [self addSubview:noteText]; or [self.view addSubview:noteText];?Denmark
Since noteText that is a UITextView is subview of the UIView class named NoteView. There I use [self addSubview:noteText], NoteView is subviewed in the UIViewController and there I use [selv.view addSubview:noteView]Yetta
Does cursor appear when you tap on you textView?Denmark
Nope. Even the keyboard won't appear. But if I hard code text, the string appear in the textview.Yetta
I think that some view lay above your textView. try to use [self bringSubviewToFront:yourTextView] and also bring all views which content yourTextView to front.Denmark
Look at my last update. Removed all other views and still not working. Have already tried bringSubviewToFront:Yetta
how you initialize your ViewController?Denmark
T
1

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.

Tamasha answered 10/10, 2012 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.